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.

In the default category widget, when you check the "show posts count" checkbox, the number of posts appear surrounded by parentheses, how to remove them.

share|improve this question
    
Personally, I would create a custom Widget that loads the categories from the database and echoing them in whatever fashion you like with custom styling and tags. For this you will need get_categories() that returns the categories in an associative array and a slightly altered version of this function to get the post count in each category. This might be an overkill for such a small change though. –  ColorWP Feb 26 '13 at 23:07
1  
I agree, the best way would be to create a custom widget, but i found a light solution to do this specific job, I posted it below :) –  Rahman Sharif Feb 26 '13 at 23:19

1 Answer 1

up vote 2 down vote accepted

Add this code to your functions.php file and it will remove the parentheses and surround the post count with a span with a class to easily style it.

function categories_postcount_filter ($variable) {
   $variable = str_replace('(', '<span class="post_count"> ', $variable);
   $variable = str_replace(')', ' </span>', $variable);
   return $variable;
}
add_filter('wp_list_categories','categories_postcount_filter');

++Bonus In the archive widget, if you checked "Show posts count" checkbox you'll see the same parentheses around posts count, here's another filter to remove them and add a class to easily style theme.

function archive_postcount_filter ($variable) {
   $variable = str_replace('(', ' ', $variable);
   $variable = str_replace(')', ' ', $variable);
   return $variable;
}
add_filter('get_archives_link', 'archive_postcount_filter');
share|improve this answer
    
Good stuff. A bit of indentation inside the function would help. –  ColorWP Feb 26 '13 at 23:21
1  
Thanks, I did it :) –  Rahman Sharif Feb 27 '13 at 0:03

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.