When dealing with a WordPress Web Site you might want to hide or exclude one or more Categories without actually deleting them: in other words, to prevent them from being shown within your Front Page Menu, Archive Pages, Feeds, search results and so on.
The best way to achieve such result is installing Ultimate Category Excluder, also known as UCE: a great Wordpress plugin that allows you to quickly and easily exclude categories from your front page, archives, feeds, and searches with a user-friendly interface. To get a glimpse of how it works, look at the screenshot below:
Unfortunately, the Ultimate Category Excluder plugin is currently unable to exclude the Categories from the WordPress built-in Category Widget, which is often the way Categories are shown within the Website's Home Page. If you want to hide a Category from everywhere, this is definitely a problem you might want to fix.
Luckily enough, it's possible to overcome such limitation with the following PHP script:
1 2 3 4 5 6 7 8 |
// Hide one or more Categories from the built-in WordPress Category Widget // https://www.ryadel.com/ function exclude_category_widget($args){ // Add the category IDs you want to exclude, separated by commas $args["exclude"] = "101,2478"; return $args; } add_filter("widget_categories_args","exclude_category_widget"); |
Add this to your WordPress theme's function.php file, enumerating the comma-separated IDs related to the Categories you want to hide, and you'll get rid of them without issues. To find the Category ID, just go to the Posts > Categories section of the WordPress Admin Panel, edit them and look at the tag_ID QueryString parameter within the destination page URL.
For additional info regarding this neat technique and/or the widget_categories_args filter, you can read the official WordPress Codex.
That's it for now... Happy coding!
This is the only solution I can find online, but it breaks my site. I added to the bottom of my child theme’s function file. Do you know of any other solution?
Thanks!
Thanks heaps!!!
I initially tried adding your code through the “Code Snippets” plugin, but it didn’t work.
I later got it to work with another code snippets plugin called “My Custom Functions” by Space X-Chimp.
:D