Remove Parentheses from Category Post Count

When you use “wp_list_categories” function to display the list of categories with post count, the parentheses shows up by default. However, if you are using this function within your custom theme, there are ways to remove the parentheses. It can be done fairly easily using following method.

// get the categories
$cat = wp_list_categories(array(
   'echo' => 1,
   'show_count' => 1,
   'title_li' => '<h3>Categories</h3>'));
   // replacing parentheses
   $clean = str_replace(array('(',')'), '', $cat);
   echo $clean;

All we are doing here is simply replacing the parentheses from the returned value and only then echoing them. Now, you might wonder what about if you are using widgets to display list of categories with their post count?! In such scenario, we can use the following method to replaces the parentheses.

function filter_post_count_parentheses($cat) {
   // replacing parentheses
   $cat = str_replace(array('(',')'), '', $cat);
   return $cat; }

In this case, we are using “add_filter” to hook “wp_list_categories” with the function to remove the parentheses. Notice that I am using the same “str_replace” (string replace) function.

You can get creative here on this specific function. Let’s say, you want to not only replace the parentheses but also want to wrap the post count with a “div” or “span” element. You can do it like this.

function replace_post_count_parentheses($cat) {
   $cat = str_replace('(', '<span>', $cat);
   $cat = str_replace(')', '</span>', $cat);
   return $cat; }

In this case we are replacing both the parentheses with the opening and closing (<span></span>) tag. You can assign class to this tag and can style them anyway you want. Hope this helps.

References: wp_list_categories, str_replace, add_filter.

Today In History




  1. krystynaPublished: 2 years ago

    Hello Iftekhar,

    Thanks for this example.
    It has taken me ages to find a solution to remove the parentheses and style the sidebar widget category list, solely due to my poor knowledge of function and class names in wordpress. I did find your example however, after finding similar solutions to this very problem, so I will post them here for anyone else who would like to see similar methods:

    1. add_filter(‘wp_list_categories’, ‘filter_list_categories’);
    function filter_list_categories($output)
    $output = str_replace(‘(‘, ‘ ‘, $output);
    $output = str_replace(‘)’, ‘ ‘, $output);
    return $output;

    2. add_filter(‘wp_list_categories’, ‘filter_list_categories’);
    function filter_list_categories($output)
    return preg_replace(‘/\\((\d+)\)\/’, ‘$1’, $output);

    I am not sure which one is more simpler/efficient.

    • Iftekhar BhuiyanPublished: 2 years ago

      krystyna, your second function will definitely generate error message. Please use the first one.

      Now, though the first method won’t generate any error message, it is highly recommended to write your function first and only then use add_filter to call that function, just like the way I wrote on my post. However, you can simplify the whole snippet like this:

      add_filter('wp_list_categories', function($output) {
          $output = str_replace('(', '', $output);
          $output = str_replace(')', '', $output);
          return $output;

      Hope that helps. Thank you for stopping by.

  2. OttoPublished: 5 years ago

    hi, im a noob in wordpress, i wanna remove parentheses from my category list count’s where i paste this code. thanks for help me.

    • Iftekhar BhuiyanPublished: 5 years ago

      Hello Otto, this snippet should be pasted on your theme’s functions.php page. Do not forget to remove the <?php and ?> from the snippet though. Otherwise, your site might break. Hope that helps. Thanks.

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.