user2684452
user2684452

Reputation: 731

Show the amount of posts in a tag in a specific category has

I am creating a page template in Wordpress that displays multiple tags that are in a particular category. I have this working, but now I want to have the number of posts within each tag that is displayed as well like if I had a tag called apples with 5 posts it would look like this:

Apples(5)

As of now it just shows Apples

Here is my code I want to modify:


    <?php
      if (is_category()){
           $cat = get_query_var('cat');
           $yourcat = get_category ($cat);
       }
       $tag_IDs = array();
           query_posts('category_name=health');
                   if (have_posts()) : while (have_posts()) : the_post();
                       $posttags = get_the_tags();
                           if ($posttags):
                           foreach($posttags as $tag) {
                               if (!in_array($tag->term_id , $tag_IDs)):
                                   $tag_IDs[] = $tag->term_id;
                                   $tag_names[$tag->term_id] = $tag->name;
                                   endif;
                       }
                       endif;
                       endwhile; endif;
                   wp_reset_query();
       
               echo "<ul>";
               foreach($tag_IDs as $tag_ID){
               echo '<a href="'.get_tag_link($tag_ID).'">'.$tag_names[$tag_ID].'</a>';
               }
           echo "</ul>";
       ?>                                  

Upvotes: 0

Views: 35

Answers (1)

amarinediary
amarinediary

Reputation: 5441

I think you're looking for the following:

Outside the loop for the current query:

<?php echo $wp_query->get_queried_object()->count; ?>

Inside the loop for each terms:

<?php echo $tag->count; ?>

Regards.

Upvotes: 1

Related Questions