Learn how to hide posts from a particular category on a WordPress theme home pages using code.
By default, some child themes display the latest blog posts on the home page, regardless of which category they belong to.
Do you want to exclude a particular category? No problem.
You can easily do this by using WordPress conditional tags.
Remember that the following snippet is created for child themes that use the front-page.php template to display the latest entries on the home page.
Code to hide a blog category on the home page
Add the following snippet to the end of the functions.php file:
To see this and another 1097 code snippets of this website, login or subscribe here.
Thanks to this snippet, entries in the category with ID 10 will not be displayed on the home page.
To find out the ID, go to:
Entries > Categories > Choose the one you want and click on edit.
If you take a look at the url, you will see something like this:
https://osomcode.com/wp-admin/term.php?taxonomy=category&tag_ID=10&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory
As you can see, in this case the category is ID 10.
If you want to hide more than one category you just have to add them to the top code separated by commas, modifying this line like this (for example):
$query->set( 'cat', '-10, -13, -55' );
If you want to exclude a category only in the blog page, take a look at this tutorial.
Conclusion
Now, thanks to WordPress conditional tags, you know how to hide a blog category (or categories) in the home page of any WordPress theme that uses the front-page.php template.
If you have any question, please leave it in the comments. And if you want to give me a suggestion for future snippets, please send it through the contact form.
Benefits of being a subscriber. 🙂