How to Remove Parentheses from Category Counts in WordPress

Home » Snippets » How to Remove Parentheses from Category Counts in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Ever wondered how you could polish the look of your WordPress website by removing the parentheses around category counts? Some WordPress themes display the number of posts in a category alongside the category name, usually within parentheses. While this functionality can be useful, you might find the look a bit disruptive. This article provides a step-by-step guide on how to remove these parentheses to streamline the aspect of your site, without impacting the essential information – the numbers themselves.

					function wpturbo_remove_parentheses_category_counts($output) {
    $output = str_replace('(', '<span>', $output);
    $output = str_replace(')', '</span>', $output);
    return $output;
}
add_filter('wp_list_categories', 'wpturbo_remove_parentheses_category_counts');
				

Let us delve into each segment of this code to understand better how it works to remove parentheses from category counts in WordPress.

The code starts by defining a function wpturbo_remove_parentheses_category_counts($output). This function accepts one argument ($output), which holds the raw html list of categories including category count with parentheses.

function wpturbo_remove_parentheses_category_counts($output) {
     // ...
}

Inside the function, we use the str_replace() function which is a string operation function available in PHP. The str_replace() function is used here to replace all ‘(‘ and ‘)’ (the parentheses encapsulating the count number) with opening and closing span tags respectively. The str_replace() function takes three parameters – the value we are looking for (old), the value to replace it with (new), and the variable in which to perform the operation (subject). The function searches the subject for all occurrences of old, and replaces them with new.

Initially, we take the variable $output, and search for all open parentheses ‘(‘, and replace them with the HTML opening span tag.

$output = str_replace('(', '<span>', $output);

Next, we run another str_replace() operation on $output – this time replacing all closing parentheses ‘)’ with the HTML closing span tag.

$output = str_replace(')', '</span>', $output);

This effectively replaces any instance of parentheses ‘(‘ and ‘)’ in your category count with opening and closing span tags, thereby giving your designer or developer freedom to design or manage it through CSS.

Finally, after these replacements, the wpturbo_remove_parentheses_category_counts() function returns the updated $output.

return $output;

On the last line of the code, we add a filter with the add_filter() function. This wp_list_categories filter hook is used to change the output of the wp_list_categories function. By default, the wp_list_categories function displays the category list with parentheses enclosing the count. With our code, whenever wp_list_categories is called, filter hook triggers our custom wpturbo_remove_parentheses_category_counts() function to replace parentheses with span tags.

add_filter('wp_list_categories', 'wpturbo_remove_parentheses_category_counts');

So, we’ve effectively used the add_filter() WordPress function to alter the default behaviour of the wp_list_categories function. The code now outputs category counts without parentheses, allowing for much more flexibility in styling and design.

Register an account to save your snippets or go Pro to get more features.