How to Show Only One Category on Your WordPress Homepage

If you’d like to show posts from only one category on your homepage, then it’s easy enough to do.

We’ll go over the steps below.

 

featured-one-cat-weekend

Find Your Category ID

First find the category ID number of the category you want to show up. You can do this by mousing over the category title (Posts > Categories), and then look in the bottom of your browser. You should see the category ID among a string of other messy info.

In this example, the category is 11.

find-cat-id

You can also use a plugin that will give you different IDs in your Admin area.

 

Insert Code

Once you have your category ID, put that into the following code, which should go into your functions file. (Appearances > Editor > Theme Functions – functions.php)

As you’ll be changing your theme’s function file, it would be wise to use a child theme.

In the example code below, I’m using category ID number 11. Replace that with your category ID.

function my_home_category( $query ) {
 if ( $query->is_home() && $query->is_main_query() ) {
 $query->set( 'cat', '11');
 }
}
add_action( 'pre_get_posts', 'my_home_category' );

If you would like to include more than one category, then just add another ID by inserting a comma and the ID number. For example, this is will display category 11 and category 14.

$query->set( 'cat', ’11, 14’ );

And that’s all there is to it. You should now only see posts on your homepage from the category you singled out in your code.

Tags

Comments (6)

Participate