如何解决排除没有分配帖子的 wp 类别
我有一个自定义帖子类型“顾问”和自定义分类“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>
解决方法
您是否尝试添加
'hide_empty' => true,
到get_terms?
$terms = get_terms(array(
'term' => 'consultants_category','hide_empty' => true,));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。