Site logo

[Resolved] How to exclude a category from displaying on Blog page?

Home Forums Support [Resolved] How to exclude a category from displaying on Blog page?

Home Forums Support How to exclude a category from displaying on Blog page?

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #704343
    Francisco

    Hi.

    I am using GP Premium 1.7.2.

    I have 10 different post categories, but I want to publish only 8 of them in the blog page. The other 2 I want to publish in a widget. How can I control the categories I want to display in the Blog page?

    #704405
    Leo
    Staff
    Customer Support
    #725015
    MÃ¥rten

    Hi,

    I tried to enter the function but it does not work for me. Could you please look into it?

    This is the function i used.

    add_filter(‘pre_get_posts’, ‘excludeCat’);
    function excludeCat($query) {
    if ( $query->is_home ) {
    $query->set(‘cat’, ‘-3,-5,-23’);
    }
    return $query;
    }

    #725064
    Leo
    Staff
    Customer Support

    You need to replace this part: 'cat', '-3,-5,-23' with the actual name of the categories you want like to exclude. If there is only one category you want to exclude, then you just need to include one quotation.

    Let me know if this makes sense 🙂

    #725076
    MÃ¥rten

    Yes, i know.

    The name of the category is Nyheter. And this is how my function looks like.

    add_filter(‘pre_get_posts’, ‘excludeCat’);
    function excludeCat($query) {
    if ( $query->is_home ) {
    $query->set(‘cat’, ‘-nyheter’);
    }
    return $query;
    }

    #725122
    Leo
    Staff
    Customer Support

    Try:

    add_filter('pre_get_posts', 'excludeCat');
    function excludeCat($query) {
          if ( $query->is_home ) {
                $query->set('Nyheter');
          }
          return $query;
    }
    #725150
    MÃ¥rten

    It does not work either.. Sorry.

    #725308
    Leo
    Staff
    Customer Support

    Sorry my mistake.

    You need to find the category ID, then replace the -3 below:

    add_filter('pre_get_posts', 'excludeCat');
    function excludeCat($query) {
          if ( $query->is_home ) {
                $query->set('cat', '-3');
          }
          return $query;
    }
    #725368
    MÃ¥rten

    Thank you!

    Now it works. 😀

    #725727
    Leo
    Staff
    Customer Support

    No problem 🙂

    #1380387
    Andrea

    I’m trying to make this work, but… Where do I need to include the function? Within the file for the Blog page or is it enough to put in the personalized CSS? Sorry for the basic question. (I suppose I need to modify the file, but before doing something like that, I prefer to ask.)

    Many thanks.

    Note: maybe it doesn’t work because it was 2018 and something has changed.

    #1380427
    David
    Staff
    Customer Support

    Hi there,

    that code is PHP, this article explains how to add that to your site:

    https://docs.generatepress.com/article/adding-php/

    #1380429
    Andrea

    Thanks, David!
    Have you ever considered to change your name in Flash? It’s always surprising how fast you guys are answering… Touché! (And happy with GP Pro!) Never felt so happy with one of my websites.

    #1380516
    David
    Staff
    Customer Support

    haha – we try our best.
    Glad to hear you’re happy with GP + GPP – feedback is much appreciated.

    #1431598
    Andrea

    Hi great guys,

    I used the “Code Snippets” plug-in it’s suggested on the page you linked, David.
    Many thanks, it worked great.

    But now I have a strange behaviour in the blog (I don’t know when it started): I cannot see the last posts. And I don’t understand where is the fault. I just added this snippet to exclude 3 categories:

    add_filter(‘pre_get_posts’, ‘excludeCat’);
    function excludeCat($query) {
    if ( $query->is_home ) {
    $query->set(‘cat’, ‘-141,-142,-143’);
    }
    return $query;
    }

    Here is the blog page:
    https://s3nzanom3.com/blog-s3nzanom3/

    As you can see, I have 3 newer posts after the last showed and they are not in the excluded categories. What’s wrong? Do you have any idea?

    Note: I posted the same problem on the “WP Show Posts” support page, sorry. I mixed two different errors there. I’m going to comment on that, because this issue seems more related to (my) GP configuration and it’s not related to show posts, as it’s the main blog page.

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.