0

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...

1

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...

0
1

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().

4
  • +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. – mor7ifer 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. – mor7ifer May 2 '12 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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