Do you want to take the control of   category display in WordPress based on whether or not it has posts?

Advertisements

WordPress does not show empty categories by default. However, you might wish to show them even if they don’t have any content.

In this article, we’ll teach you how to hide or show a category in WordPress only if it has posts.

Show or hide empty categories in WordPress

Why do you want to hide or show empty categories in WordPress?

 

WordPress includes two taxonomies by default: categories and tags. While tags are intended to identify certain subjects inside an article, categories cover a far broader range of topics.

Many WordPress websites use categories to divide their content into distinct areas. At WPBeginner, for example, we have various categories that cover the various areas of our total content.

WPBeginner categories

WordPress does not display empty categories in category widgets or category lists by default. Some website owners, however, may need to display empty categories on their WordPress blog.

Category with no posts

If they’re working on a custom theme design or simply want their visitors to see the empty categories and fill them with user-generated material, for example.

Similarly, some websites may desire to hide some categories that don’t have any posts while displaying other categories that are empty.

We’ll teach you how to simply hide or expose empty categories in WordPress in this guide.

In WordPress, you can see empty categories.

Advertisements

The new widget blocks have begun to replace the old classic widgets in WordPress. This makes changing the parameters used by the categories block to list categories a little more complicated.

We’ll need to develop our own code to list categories and then present it using shortcode to get the desired outcome.

You’ll need to add some code to your WordPress website for this lesson. Check read our guide on how to add custom code snippets in WordPress if you haven’t done so previously.

To begin, add the following code to your theme’s functions.php file or a site-specific plugin’s functions.php file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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 just lists categories using our own options, one of which is to update the hide empty category value.

After that, add the shortcode block to your widget area and then the [custom categories list] shortcode inside it to display your custom categories.

Using shortcode to display custom list of categories

When you’re done, remember to update your widget settings and visit your website to see how it works.

Displaying empty category

In your category list, hide specific categories.
Similarly, you may use the same method to exclude categories from the categories list that you don’t want to appear.

Simply change the code as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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');

We’ve concealed the categories in this code by using the exclude argument. You must also enter the IDs of the categories you want to hide.

See How to Find Category IDs in WordPress for more information.

You can now display your own categories by placing a shortcode block in your widget area and inserting the [custom categories list] shortcode inside it.

Search engines should not index category pages.

If you have empty categories on your website, you might want to hide them from search engines until you fill them with information.

Some users may choose to restrict some categories from the search engine in order to avoid duplicate content and other SEO difficulties.

You’ll need to install and activate the All in One SEO for WordPress plugin to accomplish this. See our step-by-step guide on installing a WordPress plugin for more information.

All in One SEO

The greatest WordPress SEO plugin on the market, All in One SEO for WordPress, allowing you to simply control your site’s appearance in search results.

You’ll see a setup wizard when you’ve installed the plugin. It is very self-explanatory, but if you need assistance, see our tutorial on setting up All in One SEO.

After that, go to the Posts » Categories page and click the ‘Edit’ option beneath the category you want to exclude.

Edit category

Scroll down to the All in One SEO section on the Edit Category page and select the Advanced tab.

block search engines from indexing a category page in WordPress

Turn off the ‘Default Settings’ option next to the Robots Setting from here. You now have the option of telling search engines not to index and follow this category.

When you’re finished, remember to click the ‘Update’ option to save your changes.

All in One SEO will now inform search engines that the page should not be indexed or followed.

 

Note: Search engines may still index and display posts submitted under that category in search results.

This post should have shown you how to hide or show empty categories in WordPress. You might also be interested in our WordPress guide to showing tailored content to various people and our expert pick of the top WordPress plugins for business websites.

 

 

Advertisements

Leave a Reply