Archived topic
Exclude category from loop
21 replies · Started by lock-e73 on May 30, 2015
Hi Tom,
I've recently started using the Generate Press theme, along with the GP Premium package, and really like it! So a big thanks there for starters. :)
I have a question for you though, and I apologise if it's been asked or answered before as I couldn't find reference to it in the forum. My client wants to exclude a category from the loop in index.php which is being used as the 'home' page. I have an ok grasp of php, but just wasn't sure where to include the query within the existing structure of index.php. Should I be using a child theme and adding it into index.php there?
Pretty sure it needs to be included in the
"<?php if ( have_posts() ) : ?>..."
query, but just not sure where ... ?
Or should it be a separate query prior or after including something like
"<?php if ( is_home() ) {
$query = new WP_Query( 'cat=-6' );"?
He'd like to exclude category id 6.
If you're able to help me out with this one I'd really appreciate it!
Thanks in advance,
lock-e73
Hi there,
You may be better off using a function like this:
add_filter('pre_get_posts', 'excludeCat');
function excludeCat($query) {
if ( $query->is_home ) {
$query->set('cat', '-3,-5,-23');
}
return $query;
}
Simply add your category IDs in there instead of the 3, 5 and 23 (examples). Keep the minuses in there though.
Adding PHP: http://generatepress.com/knowledgebase/adding-php-functions/
Hi Tom,
Mate, that worked a treat! Thank you so very much. Love your work! :)
Cheers,
lock-e73
You're very welcome! :)
The last response for this issue was 5 years ago. There has to be an easier way by now? Not to exclude, but just select the categories you want to how on the front/homepage.
What could be the steps now?
I didn't use GP 5 years ago, so don't know what might be different. But couldn't you just select the categories or tags you wanted to APPEAR in the "Blog/Post Listing" widget under the "Content Source" tab? You can choose one or multiple categories/tags. You can also exclude them here.
That is exactly what I want, but I don't see that option Blog/Post Listing.
https://i.postimg.cc/KcP0W2hw/Screenshot-14.jpg
You need the Pro version, I believe.
I already bought it and is active....
The feature Kerry is referring to isn't from GP. Perhaps it's coming from another plugin?
Sorry. My mistake. I purchased Elementor Pro and GeneratePress Pro at the same time and often forget they aren't one in the same.
The above function is still necessary to do this sort of thing. Altering the post loop isn't really something a theme should do by default - it's more plugin/custom function territory.
Thank you all!
Hi, is there any way to exclude a category or specific posts from related posts?
Hi there,
how are the related posts added to your site ?