Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is it possible for the_category to display a post count within a post.

Looks like Posted In: Category Name (3),Category Name (2) etc etc

Possible or not?

I would just use wp_list_categories but it needs to appear on a post by post basis like this post is posted in category x and y not just all of them...

share|improve this question

2 Answers 2

up vote 1 down vote accepted

How about this...

add_filter('get_the_categories', 'wpse50876_the_counter');

function wpse50876_the_counter($cats){
foreach($cats as $cat){
    $cat->cat_name = $cat->cat_name.'('.$cat->count.')';
}
return $cats;
}

Try it...

share|improve this answer
    
Good implementation of my solution +1 :D You should probably prefix your function names though, the_counter() is so generic it's likely to get redefined, something like wpse50876_the_counter() has a MUCH smaller chance of that happening. –  m0r7if3r May 2 '12 at 13:24
    
Hehe yeah! Changed the function name! –  Rutwick Gangurde May 2 '12 at 13:34
    
Thankyou thats brilliant! –  Jezthomp May 2 '12 at 16:45
    
You're welcome man! –  Rutwick Gangurde May 3 '12 at 5:23

You should be able to hook get_the_categories to modify the name property of each category with a count of the number of objects with that count, which should be available in the category_count property (all of this assuming that I followed the code right).

Make sure that when you hook, you add the filter, do your call to the_category() then remove it immediately, as there may be other functions that you're using which depend on get_the_category().

share|improve this answer
    
+1. I was going to suggest this - however, the filter would apply to all instances of get_the_category. This may not be an issue. Otherwise, you'll have to create your own function... –  Stephen Harris May 2 '12 at 13:03
1  
If you do something like add_filter(); the_category(); remove_filter(); it doesn't apply it to all instances, just the one you want. –  m0r7if3r May 2 '12 at 13:07
    
that's a good call :) –  Stephen Harris May 2 '12 at 13:14
    
I've found myself using that method more and more recently...it really amplifies just how powerful wordpress hooks are. –  m0r7if3r May 2 '12 at 13:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.