简体   繁体   English

如何get_terms $ count自定义分类法

[英]How to get_terms $count for custom-taxonomy

Have a custom-taxonomy called campaign_action with three categories called draft , live and paused . 有一个名为campaign_actioncustom-taxonomy其中有三个类别分别称为draftlivepaused

I would like to display just the count for each but not in a list. 我只想显示每个的计数,而不显示在列表中。

For example, I would like to echo the count for each individually as - 例如,我想将每个单独的计数回显为-

<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>

I have successfully done this by displaying 我已经成功地通过显示

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

However, this does not work for me and I want to get the $count for each individually. 但是,这对我不起作用,我想分别获取每个的$count

Thank you for your help. 谢谢您的帮助。

EDIT 编辑

To show further what I have in place currently 为了进一步展示我目前所拥有的

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

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

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

This will show all counts for each individual category but I only want to show the count for the category of draft within the custom_taxonomy of campaign_action 这将显示每个单独类别的所有计数,但是我只想在campaign_actioncustom_taxonomy中显示draft类别的计数

Here is an image of what the above achieves when added to the end of the Drafts. 这是上述内容添加到草稿末尾时所实现的图像。 I want it to only show the count for the category of drafts within the custom-taxonomy of campaign_action . 我希望它仅显示campaign_actioncustom-taxonomydrafts类别的计数。 If it has zero posts with that category then it should show zero. 如果该类别的帖子为零,则应显示零。 在此处输入图片说明

Try below code and read my comments of code. 尝试下面的代码,并阅读我的代码注释。

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

There is second way as well for custom html layout please check below code for custom html layout 自定义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;

After Your question is edited : 在您的问题被编辑后:

$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; 
    }   
}

You need to some arguments : 您需要一些参数:

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

Here is the full list of statuses in WP : https://codex.wordpress.org/Post_Status 这是WP中状态的完整列表: https : //codex.wordpress.org/Post_Status

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM