Show Post Count of a Category


I’ve been stuck on this for a little while now, and have tried multiple ways to achieve this with no luck. I’ve been looking for two ways to display the total posts found on a specific, and non-specific, category page(s). To further specify, the first I’d like to show the total number of posts for a specific (parent) category page. The second, show the total number of posts for a single category (without specifying it’s ID, name or slug).

For example, I have an index page dedicated to my parent category, "Blog Entries", which has several subcategories (Fashion, Beauty, Lifestyle, Travel, etc) that are all displayed on this page. In the title of this page I want it to display the total number of posts Blog Entries has including it’s sub categories like: Browsing all Blog Entries. 36 Posts Found. I have tried something like this:

      $categories = get_category( get_category_by_slug( 'blog-entries' )->term_id ); 
      foreach($categories as $category) { 
        $total += $category->count; 
      } echo $total . ' posts found.'; 

However, this displays the total number as 0 because it’s only specifying the category ‘blog entries’ and is not including in it’s subcategories. I’m not sure how to get it to include it’s subcategories.

For the second example, in short I have a general category/archive page that I would like to have a same thing done in the title – Browsing all [category] Entries. 6 Posts Found. I have yet to find a solution that does not specify the category. Is this possible? I don’t really want to have to make specific pages for all my categories.

Thanks in advance!

Samantha 3 months 0 Answers 7 views 0

Leave an answer