Hide or Show Category In WordPress

Hide or Show Category In WordPress

By default, WordPress doesn’t show empty categories. In some cases, you might want to show empty categories on your WordPress website. Also, you may want to hide some categories that have contents. In this article, we are going to learn How To Hide or Show Category In WordPress.

How to hide specific categories in WordPress?

You can hide any category by using the code snippet mentioned below. You need to add the below code to your active theme’s functions.php file. Please make sure to change the category ID that you want to hide in your case. In my case, I want to exclude three categories. This is why I mentioned the category IDs that are ‘7,12,33’.

function fwp_list_categories() { 
 
// define category list parameneters
$args = array (
'echo'          => false,
'title_li'      => '',
'exclude'               => '7,12,33',
'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','fwp_list_categories');

Note: To find the category ID, you need to go to your WordPress dashboard and then click Categories under the Posts section. After that click on the category name, you want to see the category ID. On the URL, you will see the category ID. Please see the screenshot below as ref.

Please make sure to use this [custom_categories_list] shortcode to display your custom category. In your WordPress blog editor, you can use the shortcode block like the screenshot below to display your custom category that you just created using the above code snippet.

How to show empty category in WordPress?

As you know by default WordPress doesn’t show empty categories. But in some cases, you may want to show empty categories. To show empty categories, you can follow the same approach and add the below code snippet to your active theme’s functions.php file.

function fwp_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','fwp_list_categories'); 

Please note functions.php is a sensitive file of your theme. Before editing this file it’s highly recommended to take a backup so that if something went wrong, you can restore the backup.

This is how we can Hide or Show Category on our WordPress website.

N.B. This is a developer-level tutorial. If you feel, it’s difficult to do for you. You may consider hiring me through Fiverr. I’ll fix your issue as soon as possible.

Table of Contents :

Leave a Comment

Your email address will not be published.