Do you want to control whether a category shows up based on whether it has any posts in WordPress?
Normally, WordPress does not display empty categories. However, there are times when you might want to show them even if they don’t have any posts.
In this article, we’ll explore simple methods to hide or show a category in WordPress, depending on whether it has any posts.
Why Hide or Show Empty Categories in WordPress
WordPress has two main ways to organize content: categories and tags. Tags are used for specific topics, while categories cover broader subjects.
Many site owners use categories to create different sections on their websites. For example, WPBeginner has categories for its main content areas.
By default, WordPress does not show empty categories in widgets or lists. However, some website owners might want to display these empty categories.
For example, they may want users to see the empty categories so they can add user-generated content, or they might be working on a custom theme design. On the other hand, some sites may prefer to hide categories without posts while still displaying other empty categories.
This guide will show you how to easily hide or show empty categories in WordPress.
How to Show Empty Categories in WordPress
WordPress now uses widget blocks instead of classic widgets, making it a bit harder to change how categories are listed. To show empty categories, you’ll need to write your own code and use a shortcode to display it.
First, add the following code to your theme’s functions.php
file or use the WPCode plugin to add the code snippet:
function wpb_list_categories() {
// Define category list parameters
$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 sets parameters to list categories and includes the hide_empty
parameter to show empty categories. After adding this code, you can display the custom categories list using a shortcode.
Add a shortcode block to your sidebar or another widget area and enter [custom_categories_list]
. Update your widget settings and visit your site to see the categories displayed.
How to Hide Specific Categories
You can also hide specific categories by modifying the above code:
function wpb_list_categories() {
// Define category list parameters
$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, the exclude
parameter hides specific categories. Replace 12,16,21
with the IDs of the categories you want to hide. You can find category IDs by following a guide on finding category IDs in WordPress.
Use the [custom_categories_list]
shortcode in a widget area to display the updated categories list.
Exclude Category Pages from Search Engines
If you show empty categories, you might want to prevent search engines from indexing them until they have content. You might also want to exclude some categories from search engines to avoid SEO issues.
To do this, install and activate the All in One SEO plugin. This plugin allows you to control how your site appears in search results.
After installing the plugin, follow the setup wizard. Then, go to the Posts » Categories page in your WordPress dashboard and click Edit under the category you want to exclude.
On the Edit Category page, scroll to the All in One SEO section and switch to the Advanced tab.
Turn off the Default Settings option next to Robots Setting. This will allow you to tell search engines not to follow or index this category. Click Update to save your settings.
All in One SEO will now tell search engines not to index or follow that category page. Note that posts in that category may still be indexed by search engines.