Do you want to show empty categories in WordPress widgets?
By default, the categories widget and block in WordPress don’t show categories that don’t have any posts assigned to them.
In this article, we’ll show you how to easily show empty categories in WordPress widgets or blocks.
Why Show Empty Categories in WordPress?
By default, the WordPress categories widget or block doesn’t show empty categories.
Empty categories are categories that don’t currently have any posts assigned to them.
However, if you are in the middle of making a website, then you may need to show empty categories in sidebar widgets or elsewhere.
That being said, let’s take a look at how to easily display empty categories in your WordPress sidebar or block widgets.
Show Empty Categories in WordPress Widgets With a Shortcode
This method is flexible and works without affecting your default WordPress widgets or blocks.
It requires you to add code to your WordPress website. If you haven’t done this before, then check out our guide on how to add custom code snippets in WordPress.
First, you need to add the following code to your theme’s functions.php file or a site-specific plugin.
function wpb_list_categories() {
// define category list parameneters
$args = array (
'echo' => false,
'title_li' => '',
'hide_empty' => 0
);
// get categories list
$display_cats = wp_list_categories($args);
//display custom categories list
return $display_cats;
}
// create shortcode
add_shortcode('custom_categories_list','wpb_list_categories');
This code simply adds a new shortcode that displays your category list and includes empty categories as well.
To use this, you need to add the shortcode widget or text widget to your WordPress sidebar.
In the widget settings, simply add the [custom_categories_list]
shortcode and save your changes.
You can now go to your website to see your categories list in action.
It will display your empty categories as well as categories with posts.
You can also use this shortcode in your WordPress posts and pages.
This method would also work if you are using older classic widgets on your website.
Simply add the shortcode to the text widget and save your changes.
Method 2. Display Empty Categories in Classic Widgets
This method can be used if you have disabled block widgets and are using classic widgets on your WordPress website.
The advantage of this method is that instead of creating the shortcode, you can modify the behavior of the default category widget.
Simply add the following code to your theme’s functions.php file or a site-specific plugin.
add_filter( 'widget_categories_args', 'wpb_force_empty_cats' );
function wpb_force_empty_cats($cat_args) {
$cat_args['hide_empty'] = 0;
return $cat_args;
}
After that, you can simply add the Categories widget to your sidebar and visit your website.
You’ll now see empty categories displayed in the categories list.
We hope this article helped you learn how to easily display empty categories in WordPress widgets. You may also want to see these handy WordPress tips, tricks, and hacks or take a look at our WordPress theme development cheat sheet for more ideas.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Faisal says
Thanks…Its working fine……!!!
Atilla Boz says
Hello and thank you. I tried this and it worked but I found out I was searching for something little different. You know when you set it the way that only one line is shown – when I set it that ways it still doen not show the empty cats. You know what I mean? If yes can you help?
Best wishes from Germany, Cologne
Wojciech Warczakowski says
Nice and simple, but I wonder what’s the purpose to show empty categories. Is there any other reason except local development?
Eric Nord says
nesting categories inside of empty categories
Wojciech Warczakowski says
You’re right. We can use empty categories for grouping other categories.
Balaji says
Nice & Thanks