Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Show / Hide Categories in WordPress (Ultimate Guide)

Do you want to display or hide a category depending on whether it has posts in WordPress?

By default, WordPress does not display empty categories. However, sometimes you may want to show them even when they have no content.

In this article, we will show easy ways to hide or display a category only if it has posts in WordPress.

Show or hide empty categories in WordPress

Why Hide or Display Empty Categories in WordPress

WordPress comes with two default taxonomies, categories and tags. While tags are used for specific topics of an article, categories are used in a much broader sense.

Many websites use categories to create different sections for their WordPress website. For instance, at WPBeginner we have different categories that cover the main sections of our overall content.

WPBeginner categories

By default, WordPress does not show empty categories in category widgets and category lists. However, some website owners may still need to show empty categories on their WordPress blog.

Category with no posts

For instance, if they are working on a custom theme design or if they simply want their users to see the empty categories and fill them with user-generated content.

Similarly, some websites may want to hide certain categories that don’t have any posts while still showing other empty categories.

In this guide, we will show you how to easily hide or display empty categories in WordPress.

Show Empty Categories in WordPress

WordPress has started using the new widget blocks replacing the old classic widgets. This makes it a bit difficult to change the parameters used by the categories block to list categories.

To achieve our desired result, we’ll need to write our own code to list categories and then display it using shortcode.

For this tutorial, you’ll need to add some code to your WordPress website. If you haven’t done this before, then checkout 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 uses our own parameters to list categories and one of these parameters is to change the hide_empty category parameter.

After that, you can display your custom categories by adding the shortcode block to your widget area and then adding the [custom_categories_list] shortcode inside it.

Using shortcode to display custom list of categories

Once you are finished, don’t forget to Update your widgets settings and visit your website to see it in action.

Displaying empty category

Hide Specific Categories in Your Category List

Similarly, you can use the same approach to exclude categories that you don’t want to be included into the categories list.

Simply modify the above code like this:

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'); 

In this code, we have hidden the categories by adding the exclude parameter. Next to it, you need to provide the IDs of categories you want to hide.

Tip: See how to find category IDs in WordPress.

You can now go ahead and display your custom categories by adding the shortcode block to your widget area and then adding the [custom_categories_list] shortcode inside it.

Exclude Category Pages from Search Engines

If you are displaying empty categories on your website, then you may want to exclude them from search engines until you have some content there.

Some users may also want to exclude some categories from search engine to avoid duplicate content and other SEO issues.

To do that, you’ll need to install and activate the All in One SEO for WordPress plugin. For more details, see our step by step guide on how to install a WordPress plugin.

All in One SEO

All in One SEO for WordPress is the best WordPress SEO plugin on the market and allows you to easily control your site’s appearance in search results.

Once you have installed the plugin, you’ll see a set up wizard. It is quite self-explanatory but if you need help, then follow our guide on setting up All in One SEO tutorial.

Next, you need to visit the Posts » Categories page and click on the ‘Edit’ link below the category that you want to exclude.

Edit category

On the Edit Category page, you need to scroll down to the All in One SEO section and switch to the Advanced tab.

block search engines from indexing a category page in WordPress

From here, you need to turn off the ‘Default Settings’ option next to the Robots Setting. You’ll now see options to tell search engines not to follow and index this category.

Once finished, don’t forget to click on the ‘Update’ button to save your settings.

All in One SEO will now start telling search engines not to index or follow that particular page.

Note: Posts filed under that category may still get indexed by search engines and will appear in search results.

We hope this article helped you learn how to hide or display empty categories in WordPress. You may also want to see our guide on how to show personalized content to different users in WordPress, and our expert pick of the best WordPress plugins for business websites.

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.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit – a collection of WordPress related products and resources that every professional should have!

Reader Interactions

10 CommentsLeave a Reply

  1. I wonder if it possible to adapt this for Taxonomy terms. So only display something if a particular Taxonomy term has posts.

  2. OMG so helpful for running and if statement to see if there are posts in a category, regular post type or custom post type. Many thanks!

    • Read the first two lines of the post. We already mention that using the wp-list-cats, yes it does, but if you are doing some more customization and need it hard coded without calling a function, then this is the way you will go.

      Admin

  3. It seems interesting but I seriously didn’t understand it. When we can name/rename a category from news to Blog. Whats the thing else than showing only if it has posts? *confused :s

    • This is only for very customized settings, but it is still good to know for those who does not. Yes you can rename a category, but how many times. If you have 7 custom page templates where you want to call one category 7 different names, then how would you go about it. For Sales / Splash pages, this will help.

      Admin

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.