最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to add a post counter to the list of custom taxonomy terms?

matteradmin4PV0评论

I am using the below code to display the list (radio buttons) of terms that are currently used by existing custom post type posts:

$args = array( 
  'taxonomy' => 'typ',
  'post_type' => 'instytucje',
);
$terms = get_terms('typ', $args);
$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '';
    foreach ($terms as $term) { 
        $i++;
        $term_list .= '<input type="radio" name="typ" value="' . $term->slug . '"> ' . $term->name . '';
        if ($count != $i) $term_list .= '<br>'; else $term_list .= '<br>';
    }
    echo '<form name="filterby" action="" method="GET">';
    echo $term_list;
    echo '<button OnClick="document.filterby.submit();">Szukaj</button>';
}

The list is used as a form to filter posts by terms.

My question is - how can I add a counter showing the number of posts next to each term on the list?

[radio button] [term name] [number of posts with the term]

This is the site I am working on:
/

I am using the below code to display the list (radio buttons) of terms that are currently used by existing custom post type posts:

$args = array( 
  'taxonomy' => 'typ',
  'post_type' => 'instytucje',
);
$terms = get_terms('typ', $args);
$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '';
    foreach ($terms as $term) { 
        $i++;
        $term_list .= '<input type="radio" name="typ" value="' . $term->slug . '"> ' . $term->name . '';
        if ($count != $i) $term_list .= '<br>'; else $term_list .= '<br>';
    }
    echo '<form name="filterby" action="" method="GET">';
    echo $term_list;
    echo '<button OnClick="document.filterby.submit();">Szukaj</button>';
}

The list is used as a form to filter posts by terms.

My question is - how can I add a counter showing the number of posts next to each term on the list?

[radio button] [term name] [number of posts with the term]

This is the site I am working on:
http://www.marketingpolityczny/baza-wiedzy/instytucje/

Share Improve this question edited May 7, 2013 at 23:51 s_ha_dum 65.6k13 gold badges84 silver badges174 bronze badges asked May 7, 2013 at 22:20 kanabikanabi 1
Add a comment  | 

1 Answer 1

Reset to default 0

Replace the 11th line with the code below (untested):

$term_list .= '<input type="radio" name="typ" value="' . $term->slug . '"> ' . $term->name . ' Number of Posts:'. $term->count;

It appears that get_terms has a post count return value.

Post a comment

comment list (0)

  1. No comments so far