最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Display empty taxonomy terms with get_terms()

programmeradmin0浏览0评论

I have a function setup as follows:

<?php $terms = get_terms("wpsc_product_category");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     foreach ( $terms as $term ) { ?>
        <li class="calendar-filter-menu-item" data-filter=".<?php echo $term->slug; ?>"><?php echo $term->count; ?></li>
    <?php }
 } ?>   

Which displays the taxonomy slug and count for each taxonomy, only problem is it's not showing a taxonomy that has no posts in, only taxonomies with posts assigned to them are being show, is it possible to show empty taxonomies as well?

I have a function setup as follows:

<?php $terms = get_terms("wpsc_product_category");
 if ( !empty( $terms ) && !is_wp_error( $terms ) ){
     foreach ( $terms as $term ) { ?>
        <li class="calendar-filter-menu-item" data-filter=".<?php echo $term->slug; ?>"><?php echo $term->count; ?></li>
    <?php }
 } ?>   

Which displays the taxonomy slug and count for each taxonomy, only problem is it's not showing a taxonomy that has no posts in, only taxonomies with posts assigned to them are being show, is it possible to show empty taxonomies as well?

Share Improve this question edited Sep 11, 2014 at 11:31 Nicolai Grossherr 18.9k8 gold badges64 silver badges109 bronze badges asked Sep 11, 2014 at 11:14 user1374796user1374796 39511 gold badges18 silver badges30 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 8

You can make use of the hide_empty argument of get_terms(). It's default value is set to true.

Do it somewhat like this:

$args = array(
    'hide_empty' => false
);
$terms = get_terms( 'wpsc_product_category', $args );

If you are using string request mode, use "0" instead of "false" :

$terms = get_terms('hide_empty=0');
发布评论

评论列表(0)

  1. 暂无评论