How to Show Only One Category on the WordPress Homepage

HomeWordPressHow to Show Only One Category on the WordPress Homepage

How to Show Only One Category on the WordPress Homepage

There are some cases where WordPress users want to show only a single category on the homepage of their website. For instance, photographers who build a WordPress-based site to market their works want to show only photos under the “featured” category to show on the homepage of their website.

You need to edit the WordPress code to hide all categories from the homepage but the one you want to display on the homepage. The file you would be editing is functions.php. The point of this step is that you add a new function on the functions.php fileto instruct WordPress to hide all categories from the homepage with the exception of the one you want to display on the homepage. Before doing so, you need to know the ID of the category you want to display on the homepage first.

Finding the category ID

To find the ID of the category you want to display on your WordPress homepage, login to the WordPress dashboard and go to the Categories menu.

Hover your mouse over the category you want to show up and look on the bottom of your browser. You can find the category ID among the string.

Editing the functions.php file

Once you found the ID of the category you want to show up, you can start editing the functions.php file of WordPress. On the WordPress dashboard, go to Appearance -> Editor. On the Editor page, select the functions.php file by clicking it.

Add the following code to the functions.php file. Don’t forget to replace the category ID.

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

Following is the example.

After adding the code above, don’t forget to click the Update File button to apply the change. If you want to display more than one categories on the WordPress homepage, simply add the IDs of the categories you want to show up and separate them with comma. Example:

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

Just in case if everything is not going the way it should be, don’t forget to backup the functions.php file before you edit it.

hand-picked weekly content in your inbox

.

related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here