WordPress: Remove or replace parentheses from Category Counts (wp_list_categories)

You can learn the basics about the wp_list_categories() function from the WordPress Codex.

By default this function will not show up the post count, but if you will activate it by show_count=1, the post count will be surrounded by parentheses. To remove the parentheses without modifying the WordPress core files, you need to use the following code:

<?php
$variable = wp_list_categories('echo=0&show_count=1&include=1,2,3,4,5&title_li=');
$variable = str_replace(array('(',')'), '', $variable);
echo $variable;
?>

Please note in the example above the title_li parameter is set to hide the title or heading for the category list generated by wp_list_categories().

So what if you will want not only to remove the parentheses, but to replace them with some other html markup? The solution is the next code:

<?php
$variable = wp_list_categories('echo=0&show_count=1&include=1,2,3,4,5&title_li=');
$variable = str_replace('(', '<sup>', $variable);
$variable = str_replace(')', '</sup>', $variable);
echo $variable;
?>

By using the str_replace php function, now instead of parentheses you can have the sup html tag or any other html tag based on the requirements. The code above will list the categories with the id’s from 1 to 5 without a title or header and will show the post count in a superscript format. Will look like the next example:

wordpress category list WordPress: Remove or replace parentheses from Category Counts (wp list categories)

Thank you!

Tags: , , , ,

Leave a Reply