How to Force the Categories Widget to Display Empty Categories in WordPress

Home » Snippets » How to Force the Categories Widget to Display Empty 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 frustrated with the default behavior of the categories widget in WordPress, where it only displays categories that actually contain posts? If you want to show all categories, even the empty ones, in your widget, there is a solution. In this article, we will guide you through the process of forcing the categories widget to display empty categories in WordPress. By implementing this tweak, you will have more control over your widget and be able to provide a comprehensive overview of all available categories, regardless of whether they currently have any posts or not.

					function wpturbo_force_empty_categories() {
    $args = array(
        'hide_empty' => 0
    );
    return $args;
}
add_filter( 'widget_categories_args', 'wpturbo_force_empty_categories' );
				

The code snippet above addresses the issue of the categories widget not displaying empty categories in WordPress. By default, the categories widget will only show categories that have posts assigned to them. However, if you want to display all categories, including those without any assigned posts, you can use this code.

The function wpturbo_force_empty_categories() is responsible for modifying the arguments passed to the categories widget. Within this function, we define an array named $args that consists of a single argument: 'hide_empty' set to 0. Setting 'hide_empty' to 0 instructs the widget to display all categories, regardless of whether they have any posts.

$args = array(
    'hide_empty' => 0
);

Next, we use the add_filter() function to hook the wpturbo_force_empty_categories() function into the widget_categories_args filter. This filter allows us to modify the arguments passed to the categories widget specifically. By doing this, we can override the default behavior of hiding empty categories.

add_filter( 'widget_categories_args', 'wpturbo_force_empty_categories' );

When this code is implemented, the categories widget will display all categories on the frontend, including those without any assigned posts. This can be particularly helpful if you want to provide visitors with a comprehensive overview of all available categories, regardless of their usage.

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