- This topic has 21 replies, 4 voices, and was last updated 3 years, 6 months ago by Leo.
-
AuthorPosts
-
May 30, 2015 at 3:50 pm #111504lock-e73
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
May 31, 2015 at 12:13 am #111543TomLead DeveloperLead DeveloperHi 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/
May 31, 2015 at 3:16 pm #111679lock-e73Hi Tom,
Mate, that worked a treat! Thank you so very much. Love your work! π
Cheers,
lock-e73
May 31, 2015 at 11:52 pm #111708TomLead DeveloperLead DeveloperYou’re very welcome! π
February 16, 2020 at 9:22 am #1167311YolandaThe 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?
February 16, 2020 at 9:48 am #1167326KerryI 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.
February 16, 2020 at 10:06 am #1167341YolandaThat is exactly what I want, but I don’t see that option Blog/Post Listing.
https://i.postimg.cc/KcP0W2hw/Screenshot-14.jpgFebruary 16, 2020 at 10:12 am #1167347KerryYou need the Pro version, I believe.
February 16, 2020 at 10:21 am #1167358YolandaI already bought it and is active….
February 16, 2020 at 10:26 am #1167362LeoStaffCustomer SupportThe feature Kerry is referring to isn’t from GP. Perhaps it’s coming from another plugin?
February 16, 2020 at 10:44 am #1167380KerrySorry. My mistake. I purchased Elementor Pro and GeneratePress Pro at the same time and often forget they aren’t one in the same.
February 16, 2020 at 7:52 pm #1167580TomLead DeveloperLead DeveloperThe 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.
February 17, 2020 at 12:47 am #1167681YolandaThank you all!
September 16, 2020 at 4:46 am #1445512OlaniyiHi, is there any way to exclude a category or specific posts from related posts?
September 16, 2020 at 5:38 am #1445574DavidStaffCustomer SupportHi there,
how are the related posts added to your site ?
-
AuthorPosts
- The topic ‘Exclude category from loop’ is closed to new replies.