我正在尝试显示特定类别(分类)为”
Book1″
的帖子。
我试图用以下代码显示它。
$args = array(
'post_type' =>
'book', 'posts_per_page' =>
6, 'tax_query' =>
array(
array(
'taxonomy' =>
'Book1', 'field' =>
'id', 'terms' =>
1
)
)
);
echo '<
br>
';
$postss = get_posts( $args );
if ( ! empty( $postss ) &
&
is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->
post_title. '<
br>
';
$i++;
}
}
?>
在输出中, 不显示任何项目。
#1
$custom_terms = get_terms('Book1');
foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
'post_type' =>
'book', 'posts_per_page' =>
6, 'tax_query' =>
array(
array(
'taxonomy' =>
'Book1', 'field' =>
'slug', 'terms' =>
$custom_term->
slug, ), ), );
$loop = new WP_Query($args);
if($loop->
have_posts()) {
echo '<
h2>
'.$custom_term->
name.'<
/h2>
';
while($loop->
have_posts()) : $loop->
the_post();
echo '<
a href="'.get_permalink().'">
'.get_the_title().'<
/a>
<
br>
';
endwhile;
}
}
试试这个代码
#2
$args = array(
'post_type' =>
'book', 'posts_per_page' =>
6, 'tax_query' =>
array(
array(
'taxonomy' =>
'Book1', 'field' =>
''term_id', // here you are worng name too
'terms' =>
1
)
)
);
echo '<
br>
';
$postss = get_posts( $args );
if ( ! empty( $postss ) &
&
is_array( $postss ) ) {
// Run a loop and print them all
$i=1;
foreach ( $postss as $termm ) {
echo ' '.$i.' '.$termm->
post_title. '<
br>
';
$i++;
}
}
?>
//最佳解决方案
<
?php$query = new WP_Query( array(
'post_type' =>
'book', // name of post type.
'tax_query' =>
array(
array(
'taxonomy' =>
'Book1', // taxonomy name
'field' =>
'term_id', // term_id, slug or name
'terms' =>
1, // term id, term slug or term name
)
)
) );
while ( $query->
have_posts() ) : $query->
the_post();
// do stuff here....
endwhile;
/**
* reset the orignal query
* we should use this to reset wp_query
*/
wp_reset_query();
?>
#3【如何在WordPress中根据类别(分类)检索帖子(书)()】我是这样做的。谢谢大家
$args = array(
'post_type' =>
'book', 'cat' =>
'35'
);
echo '<
br>
';
$postss = query_posts($args);
if ( ! empty( $postss ) &
&
is_array( $postss ) ) {
// Run a loop and print them all
?>
<
?php $i=1;
foreach ( $postss as $termm ) { ?>
<
?php echo ' '.$i.' '.$termm->
post_title. '<
br>
';
$i++;
?>
<
?php
}
}
推荐阅读
- 如何通过wordpress API端点返回内容
- 如何解决错误(使用一键导入WordPress导入数据时,内部服务器错误(500)())
- 如何删除woocommerce选项卡()
- 操作系统Kali 2021 改中文
- linux cp复制软连接
- 「官宣」摹客XJira,项目管理星搭档!
- 初识分支与循环
- Linuxyum install 没有可用软件包 解决办法
- vivo全球商城-营销价格监控方案的探索