0

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

4

2 回答 2

4

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; 
    }   
}
于 2017-10-09T07:28:49.320 回答
-1

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

于 2017-10-09T07:30:53.233 回答