Exclude Categories from Category Widget in WordPress
Updated On May 27, 2017 June 3, 2015 -Written ByWordPress by default has a “Categories” widget to display list of all categories. This widget has options like: display as dropdown, show post counts and show hierarchy. Do you want to remove one or multiple categories from displaying when using this category widget? There is no custom option for such removal but some custom code can do the magic.
Code to remove & exclude specific categories
In your functions.php file add the following code.
https://gist.github.com/5c08df65dc3aca6f7244
In the above code: 3,4 are IDs of categories that should be excluded (not to be displayed). You can add more category IDs separated by comma as per requirement.
When using Category dropdown box
When using the category widget in the form of a drop down box, above code won’t work. You can need to use the following code.
https://gist.github.com/8f9578fe5d317a1b1efe
Above, we are just replacing widget_categories_args with widget_categories_dropdown_args in the code.
Very nice snippet, will definitely add this to my sites for at least “Uncategorized.” One thought: Might want to add a code sample to this post for those who want to remove a category regardless of whether dropdown is used or not. Kinda obvious now, but putting them both in without looking too closely gets you the white screen of death.
//* Exclude Categories from Category Widget
function custom_category_widget($args) {
$exclude = “1”; // Category IDs to be excluded
$args[“exclude”] = $exclude;
return $args;
}
add_filter(“widget_categories_args”,”custom_category_widget”);
add_filter(“widget_categories_dropdown_args”,”custom_category_widget”);