doudui2229 2017-10-09 07:20
浏览 31
已采纳

如何get_terms $计入自定义分类

Have a custom-taxonomy called campaign_action with three categories called draft, live and paused.

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.

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

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. If it has zero posts with that category then it should show zero. enter image description here

  • 写回答

2条回答 默认 最新

  • doujiu1447 2017-10-09 07:28
    关注

    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

    $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; 
        }   
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?