Find out how to display only the posts belonging to one specific category in the home of WordPress themes using code.
By default, some child themes display the latest blog posts on the home page, regardless of which category they belong to.
But you may want to promote a particular category on the home page to strategically direct visitors.
You can do this easily by using WordPress conditional tags.
Remember that the following snippet is created for themes that use the front-page.php template to show the latest entries on the home page.
Code to show only 1 blog category on the home page
Add the following fragment at 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, only the entries of the category with ID 10 will be displayed on the main 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 it’s ID 10.
If you want to enter more than one category you just have to add them to the top code separate by commas, modifying this line like this (for example):
$query->set( 'cat', '10, 13, 55' );
However, depending on the amount, you may be more interested in excluding certain categories, as I explain in this tutorial.
Conclusion
Now, thanks to WordPress conditional tags, you know how to display only 1 blog category in the home page of any WordPress theme that uses the front-page.php template.
Any questions? I read you in the comments.
And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉