如何get_terms $ count自定义分类

时间:2017-10-09 07:20:43

标签: php wordpress get taxonomy-terms

custom-taxonomy名为campaign_action,其中包含三个类别draftlivepaused

我想只显示每个但不在列表中的计数。

例如,我想将每个单独的计数回显为 -​​

<li>Draft (<?php //code to display a number count for drafts ?>)</li>
<li>Live (<?php //code to display a number count for live ?>)</li>
<li>Paused (<?php //code to display a number count for paused ?>)</li>

我已成功完成此操作

foreach ( $terms as $term ) {
   echo '(' . $term->count . ')';
 }

但是,这对我不起作用,我希望每个人都获得$count

感谢您的帮助。

修改

进一步展示我现有的资料

    <?php  
$terms = get_terms('campaign_action');

 if ( !empty( $terms ) && !is_wp_error( $terms ) ){

 echo '(0)';
 foreach ( $terms as $term ) {
   echo '(' . $term->count . ')';
 }
 } 
?> 

这将显示每个类别的所有计数,但我只想显示draft custom_taxonomycampaign_action类别的计数

以下是添加到草稿末尾时上述内容的图像。我希望它仅显示drafts custom-taxonomycampaign_action类别的计数。如果它具有该类别的零帖子,那么它应该显示为零。 enter image description here

2 个答案:

答案 0 :(得分:3)

尝试以下代码并阅读我对代码的评论。

echo wp_list_categories( array(
    'orderby'    => 'name',
    'show_count' => true,
    'taxonomy'   => 'campaign_action' //i guess campaign_action  is your  taxonomy 
) );

自定义html布局还有第二种方法请查看以下代码以获取自定义html布局

$terms = get_terms(array(
 'taxonomy' => 'campaign_action',//i guess campaign_action  is your  taxonomy 
 'hide_empty' => false
));
echo $terms->name;
echo $terms->count;

编辑完问题后

$terms = get_terms(array(
'taxonomy' => 'campaign_action',//i guess campaign_action  is your  taxonomy 
'hide_empty' => false
));
foreach ($terms as $terms)
{
    if($terms->name == 'Draft') 
    {
        echo $terms->name;
        echo $terms->count; 
    }   
}

答案 1 :(得分:0)

你需要一些论点:

<?php
$args = array(
     'post_type' => 'campaign_action',
     'post_status' => 'publish' //(Or Draft...etc)
);
$show_recipes= get_posts( $args );
echo $show_recipes->post_count;
?>

以下是WP中的完整状态列表:https://codex.wordpress.org/Post_Status