Unveiling Empty Categories: A Complete Guide for WordPress Sidebar
By default, WordPress sidebars do not display empty categories. However, there may be situations where you want to showcase empty categories, especially when setting up a new website. In this article, we will provide a step-by-step guide on how to easily show empty categories in your WordPress sidebar. Whether you prefer using code snippets or plugins, we have you covered.
Method 1: Using the WPCode Extension
Instead of adding code snippets to your theme’s functions.php file, it is recommended to use the WPCode extension to avoid any potential risks that could break your website. WPCode is a popular code snippet plugin that allows you to add custom code to your WordPress site safely and easily.
Follow these instructions to get started:
Install and activate the WPCode plugin. You can refer to our detailed guide on how to install a WordPress plugin for more information.
Once activated, go to the “Code Snippets” section and click on “+Add Snippet”. This will take you to the snippet library, which contains a variety of pre-tested shortcodes.
Click on “Add Your Custom Code (New Snippet)” and give a title to the snippet, such as “Empty Categories”.
Change the Code Type to “PHP Snippet” and paste the following code into the Code Preview box:
function custom_category_list($args = '') {
$custom_args = array(
'hide_empty' => 0,
'title_li' => ''
);
$args = wp_parse_args($args, $custom_args);
return wp_list_categories($args);
}
add_shortcode('custom_categories', 'custom_category_list');
Toggle the switch from “Inactive” to “Active” in the top right corner and click “Save Snippet”.
Scroll down to the “Insertion box”, select the “Shortcode” option next to “Insert Method”, and copy the generated shortcode.
Go to “Appearance ≫ Widgets” in your WordPress admin area. Find the “Main Sidebar” section, click the “+” icon, and choose “Shortcode”.
Paste the shortcode generated by WPCode into the box and click the “Update” button.
Now, you can preview your website to see the list of categories, including empty categories, in action. This shortcode can also be used in WordPress posts and pages, making it convenient for visitors to browse related topics by category.
Method 2: Modifying the Default Category Widget
This method is useful if you are using classic widgets on your WordPress website and have deactivated block widgets. Instead of creating a shortcode, you can modify the behavior of the default category widget.
Follow these steps to implement this method:
Install and activate the WPCode plugin.
Repeat the same steps as Method 1, including the following code in WPCode:
function display_empty_categories($cat_args) {
$cat_args['hide_empty'] = 0;
return $cat_args;
}
add_filter('widget_categories_args', 'display_empty_categories');
Add the widget to your sidebar by going to “Appearance ≫ Widgets”. Copy the shortcode generated by WPCode and paste it into a text widget. Save your changes.
Visit your live website to see the modifications. Your list of categories should now include empty categories.
Conclusion
By following the methods outlined in this complete guide, you can easily show empty categories in your WordPress sidebar. This functionality offers greater flexibility when organizing and displaying your website’s content. For more WordPress tips, tricks, and hacks, or for further insights into WordPress theme development, feel free to explore our wide range of resources. Don’t forget to subscribe to our YouTube Channel for engaging WordPress video tutorials and connect with us on Twitter and Facebook.