Are you looking to show or hide a category based on whether it contains posts in WordPress?
By default, WordPress does not show empty categories. However, there may be instances when you wish to exhibit them even when they do not have content. This article will provide you with straightforward methods to either hide or reveal a category exclusively if it has posts in WordPress, without much hassle.

Why Hide or Show Categories in WordPress
WordPress includes two pre-built taxonomies: categories and tags. While tags are utilized to identify particular subjects in an article, categories have a more general application.
Numerous websites use categories to establish various sections for their WordPress sites. For instance, at Harmonweb, we have various categories that cover the primary areas of our overall content. WordPress doesn’t display empty categories in category widgets and lists by default. However, some website owners may want to exhibit empty categories on their WordPress blog. This could be necessary when working on a custom theme design, or when they want their users to see the empty categories and add user-generated content to them.
Furthermore, some websites may want to hide certain categories that have no posts while revealing other empty categories. In this guide, we will provide easy-to-follow instructions on how to hide or show empty categories in WordPress.
Show Empty Categories in WordPress
WordPress has recently introduced a new widget block system to replace the classic widgets. However, modifying the parameters of the categories block using this new system can be challenging.
To address this, one option is to create custom code that can list categories and then use a shortcode to display it on your site.
To start, add the following code to your site-specific plugin or theme’s functions.php file:
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 utilizes custom parameters to list categories, including the ability to adjust the hide_empty category parameter.
Once you’ve added the code, you can display your custom categories by adding the shortcode block to your widget area and inserting the [custom_categories_list] shortcode. Afterward, update your widget settings and visit your website to see your custom category list in action.
Hide Specific Categories in Your Category List
You can employ a similar method to exclude specific categories from the category list if desired.
To do so, add the code as follows:
function wpb_list_categories() {
// define category list parameneters
$args = array (
'echo' => false,
'title_li' => '',
'exclude' => '12,16,21',
'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');
add the exclude parameter to the code and input the IDs of the categories you wish to conceal.
Once you’ve made these modifications, you can display your customized category list by inserting the [custom_categories_list] shortcode within the shortcode block in your widget area.
Exclude Category Pages from Search Engines
If your website has empty categories, it’s best to exclude them from search engines until you add content. This not only helps with SEO issues but also provides a better user experience for visitors.
To accomplish this, you can use the All in One SEO for WordPress plugin. This is a powerful SEO tool that allows you to control how your site appears in search results. To get started, install and activate the plugin using our step-by-step guide on installing WordPress plugins.
Once you’ve installed the plugin, you’ll see a setup wizard that guides you through the process. If you need assistance, our guide on setting up All in One SEO can provide helpful tips.
Next, go to the Posts » Categories page and select the category you want to exclude.

Click on the ‘Edit’ link below that category, and scroll down to the All in One SEO section. Switch to the Advanced tab and turn off the ‘Default Settings’ option next to the Robots Setting.

You’ll then see options to tell search engines not to follow or index that category.
Finally, click the ‘Update’ button to save your settings, and All in One SEO will begin excluding the category from search engine results.
If you liked this article, then please subscribe to our YouTube Channel for more WordPress video tutorials. You can also find us on Facebook.