我有一个自定义的帖子类型'consultants‘和自定义分类'consultants_category’。我需要输出所有自定义分类(类别),这些分类都分配了帖子。但是上面的代码输出了所有的分类,包括那些没有分配帖子的分类- https://prnt.sc/zr96jh请帮助我改进这段代码。谢谢。
<div class="cp_allconsultants-l1_list_new">
<?php
$terms = get_terms(array(
'term' => 'consultants_category',
));
foreach( $terms as $term ):
?>
<div class="l2posts_by_cat">
<h3><?php echo $term->name; ?></h3>
<ul>
<?php
$posts = get_posts(array(
'post_type' => 'consultants',
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'nopaging' => true,
));
foreach($posts as $post):
setup_postdata($post);
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>发布于 2021-02-17 02:18:30
你有没有试过添加
'hide_empty' => true,对get_terms?
$terms = get_terms(array(
'term' => 'consultants_category',
'hide_empty' => true,
));https://stackoverflow.com/questions/66229953
复制相似问题