How to Remove After wp_list_categories in WordPress

WPTurbo » Snippets » How to Remove After wp_list_categories 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

Are you a WordPress developer looking to customize your website’s category listings? One common modification is to remove certain categories from the wp_list_categories function output. This might seem like a daunting task for some, especially those new to WordPress development. However, we’ve got you covered. In this article, we will guide you through the process of effectively removing unwanted categories from your wp_list_categories in a few, simple steps.

					function wpturbo_remove_after_categories($output) {
    $output = str_replace('</li> (', '</li> <span style="display:none;">(', $output);
    $output = str_replace(')', ')</span>', $output);
    return $output;
}
add_filter('wp_list_categories', 'wpturbo_remove_after_categories');
				

The initial line of the code snippet declares a new function titled wpturbo_remove_after_categories($output). This function is designed to modify the output of the wp_list_categories function in WordPress.

The wp_list_categories function is a built-in WordPress function that displays a list of categories as links. When you call wp_list_categories, it generates a list, and each item in the list is wrapped in list (<li>) HTML tags. After each listed category, the function includes the number of posts in that category. The number itself is wrapped in parentheses, like so: (4). This new function we are creating, wpturbo_remove_after_categories($output), will alter this original output from wp_list_categories.

The function accepts an argument $output, which contains the original HTML generated by the wp_list_categories function.

Moving forward to the function body, we find two lines of str_replace functions. The purpose of these is to change the layout of the list generated by wp_list_categories.

$output = str_replace('</li> (', '</li> <span style="display:none;">(', $output);
$output = str_replace(')', ')</span>', $output);

str_replace is a built-in PHP function that searches for a specific string within another string and replaces it with a third string. Its syntax is str_replace(find, replace, string).

In both lines, we’re utilizing str_replace to seek out the parentheses and wrap them within a span tag with a style attribute set to display:none. This implies that the post count that usually follows each category in brackets will continue to be present in the webpage’s HTML but will not be displayed visually because the CSS property display:none hides the content.

Upon altering the $output, we return the modified version. This updated $output will replace the default WordPress category list.

The final part of the code snippet hooks our newly created function into the wp_list_categories filter using add_filter. add_filter is a WordPress function we use to modify different parts of WordPress, including the output of other functions such as wp_list_categories.

add_filter('wp_list_categories', 'wpturbo_remove_after_categories');

This means, whenever wp_list_categories is called in the theme, our function wpturbo_remove_after_categories is executed first, modifying the output before it is displayed on the screen.

To summarize, this function enables you to hide the post count that automatically follows each category listing in WordPress wp_list_categories, providing a cleaner look to your category list.

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