[Resolved] Exclude category from loop

Home Forums Support [Resolved] Exclude category from loop

Home Forums Support Exclude category from loop

Viewing 15 posts - 1 through 15 (of 22 total)
  • Author
    Posts
  • #111504
    lock-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

    #111543
    Tom
    Lead Developer
    Lead Developer

    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/

    #111679
    lock-e73

    Hi Tom,

    Mate, that worked a treat! Thank you so very much. Love your work! πŸ™‚

    Cheers,

    lock-e73

    #111708
    Tom
    Lead Developer
    Lead Developer

    You’re very welcome! πŸ™‚

    #1167311
    Yolanda

    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?

    #1167326
    Kerry

    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.

    #1167341
    Yolanda

    That is exactly what I want, but I don’t see that option Blog/Post Listing.
    https://i.postimg.cc/KcP0W2hw/Screenshot-14.jpg

    #1167347
    Kerry

    You need the Pro version, I believe.

    #1167358
    Yolanda

    I already bought it and is active….

    #1167362
    Leo
    Staff
    Customer Support

    The feature Kerry is referring to isn’t from GP. Perhaps it’s coming from another plugin?

    #1167380
    Kerry

    Sorry. My mistake. I purchased Elementor Pro and GeneratePress Pro at the same time and often forget they aren’t one in the same.

    #1167580
    Tom
    Lead Developer
    Lead Developer

    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.

    #1167681
    Yolanda

    Thank you all!

    #1445512
    Olaniyi

    Hi, is there any way to exclude a category or specific posts from related posts?

    #1445574
    David
    Staff
    Customer Support

    Hi there,

    how are the related posts added to your site ?

Viewing 15 posts - 1 through 15 (of 22 total)
  • The topic ‘Exclude category from loop’ is closed to new replies.