获取当前的分类法计数

时间:2014-05-21 09:59:55

标签: wordpress taxonomy

有没有办法让当前的分类(类别或标签)计数?我所拥有的代码仅适用于类别

<?php 
    $cat = get_query_var( 'cat' );
    $categories = get_categories( 'include='.$cat );
    if ( $categories ) { 
        foreach( $categories as $category ) { 
            echo '' . $category->count;
        }
    }
?>

1 个答案:

答案 0 :(得分:1)

我想你可以这样做:

// Set the name of your Taxonomy or get it as you're currently doing
// It can be category, tag or custom taxonomy name
$taxonomy = "your_taxonomy"; 

$total_count = 0;

// Get all the terms in your Taxonomy and add the count for each term
foreach ( get_terms( $taxonomy ) as $term ) {
    $total_count += (int) $term->count;
}

echo $total_count;

这将为您提供在分类your_taxonomy中分配了任意期限的所有帖子的计数,我明白这就是您想要的...