How To Hide Categories From The Homepage In WordPress

How To Hide Categories From The Homepage In WordPress
In WordPress we have a tool that helps us organize content, so users can find what they are looking for in a faster way.

This tool is the categories, but, we may have too many, and we don’t want to show them all on the main page.

Remember that categories are used to divide our content according to topic. So a category will only show the posts it contains.

On our home page we do not want to show all categories, for many reasons, to make our site more attractive, increase SEO, accelerate loading speed, reducing content.

WordPress will not let us hide categories by default, so we will have to find a solution.

There are two methods to exclude a category, through a plugin, or in a way for more advanced users, the code.

Hide Categories From The Homepage Using Plugin

The first thing we are going to do is go to the WordPress Dashboard and go to the Plugins → Add New. We look for the Category Excluder plugin.
Category Excluder plugin WordPress

We install and activate the plugin, and to access its configuration, within the WordPress Dashboard we go to Settings → Category Excluder.
Category Excluder plugin settings WordPress

Here we can mark different options, we can exclude different categories, and on the page where we do not want them to load, the main page, RSS feeds, the search …

Once the configuration is finished, click the Update button to save the changes.

Hide Categories From The Homepage Using Code

In this case we are going to add code to WordPress, which is intended for advanced users, although it is not difficult, but we can spoil our WordPress.

To perform this action, we must add the following code to our “functions.php” file.

[php] function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-1’ );
}
return $query;
}

add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );
[/php]

The ID (-1), with the ID of the category that we want to hide. It will hide all the posts that belong to that category of the blog, and that match the ID.

The minus symbol should be kept, since we want to hide that category.

We can also hide more than one category at the same time, simply adding the IDs that we want not to be shown.

[php] function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( ‘cat’, ‘-1, -2, -3’ );
}
return $query;
}

add_filter( ‘pre_get_posts’, ‘exclude_category_home’ );
[/php]

This is how easy we can remove the categories we want from the home page, so as not to overload our home page, and make it more user-friendly for our users.

How to Find a Category ID

Go to Posts → Categories and click Edit on the category. In the address bar of your browser you will see tag_ID= followed by a number. That number is the ID you need in the code.

A Safer Version of the Code

The code above works, but it is better to make sure it only changes the main blog query and does not touch other queries on your site. This version does that:

function exclude_category_home( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-2,-3' );
    }
}
add_action( 'pre_get_posts', 'exclude_category_home' );

Replace 1, 2 and 3 with the IDs of your categories and keep the minus sign in front of each one.

Where to Add the Code

Add it to the functions.php file of a child theme, or use a code snippets plugin. If you edit the functions.php of your main theme, an update will erase your changes, and a mistake in the code can break your site, so make a backup first.

What This Does Not Do

  • The posts are only hidden on the page that shows your latest posts. They will still appear in their category page, in search results and on their own URL unless you also exclude them there.
  • The plugin has options to exclude categories from feeds, archives and search as well, which the code above does not do.
  • If your homepage is a static page, this code has no effect, because it changes the blog posts page, not a page you built with a page builder.

If you have not created your website yet, you may be interested Create Your Website With WordPress.

Looking for a design that puts your writing first? Take a look at our WordPress blog themes. You may also like: How to Add Posts to WordPress Navigation Menu.

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Shares
Share This