Exclude category posts from main blog loop on GeneratePress

  •  
  •  
  •  
  •  
  •  

Last updated on January 19th, 2018 at 03:17 pm

The free theme GeneratePress is a great base for your next WordPress project. It is fast, lightweight and very well coded. Plus the provided support is more than excellent.

So, let’s say you are using it to design your next or first blog. Very often it might happen that you would like to exclude certain categories from the main loop on your blog page.

There is an easy way to do this in GeneratePress.

With a child theme

The recommended way is to generally use a child theme. Then you should simply paste the code into the GP’s child theme.

It must be pasted in functions.php. Connect to your website via FTP. Look inside wp-content > themes > ‘your-GeneratePress-child-theme-name’ > functions.php.

functions.php

This is the code you should paste.

 add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-8,-14');
}
return $query;
}

The numbers are showing the category IDs of the those you want to exclude. To find the ID of each category, go to Posts >> Categories and click on the desired category. In your browser URL, you will see the ID.

cat-id-tag

Without a child theme

Using a plugin

If you are not using a child theme with GeneratePress, then you would need to install a code snippet plugin from the WordPress repository. The plugin will allow you to insert additional code snippets without losing any changes when your theme is updated.

I would recommend using this one – Code Snippets. Go to your WordPress admin are Plugins >> Add new and search for it.

Creating your own plugin

There is a third way of applying extra code by creating your own plugin. If you are not an experienced WordPress user, it is better to leave that option out.

If you would like to try this out or train it, read more about it here.

More about GeneratePress

You might be also interested to read how to enable Yoast breadcrumbs in GeneratePress. It is also an excellent choice if you want to build a blog fast.


  •  
  •  
  •  
  •  
  •  

Leave a Comment