[Support request] Exclude a category from "Previous Next articles"

Home Forums Support [Support request] Exclude a category from "Previous Next articles"

Home Forums Support Exclude a category from "Previous Next articles"

  • This topic has 30 replies, 4 voices, and was last updated 2 years ago by Leo.
Viewing 15 posts - 1 through 15 (of 31 total)
  • Author
    Posts
  • #899745
    Elia

    Hi there,

    Could you please help me exclude the category from the “Previous Next articles” section?

    #899791
    Elia

    By exclude I meant 2 things.

    1) When you go to the blog and go to the previous/next articles you shouldn’t see links to 1 of the categories
    2) However, when we open this category directly on the website you should be able to other link within this categories using the previous/next articles section

    Please let me know if I’m nor clear

    #899946
    David
    Staff
    Customer Support

    Hi there,

    you can remove the category link in Customizer > Layout > Blog –> Single Tab > Display Post Cateogries

    To limit the scope of the post links to the current category you can use the filter that Tom provides here:

    https://generatepress.com/forums/topic/behavior-of-next-previous-post-links/#post-884910

    #899963
    Elia

    Thanks, David!

    However, this is not what we need.

    Let’s say we have 4 categories:
    Category A links
    Category B links
    Category C links
    Category D links

    We would like to isolate all links within one of the categories.

    To do so we need 2 things:

    1) Exclude Category C links from the previous/next section when you visit posts from other categories. As a result when let’s say you visit a post from the Category A, you won’t see a link to Any post leading to Category C in the previous/next section
    2) This step is OPTIONAL (If it’s hard to do then just the first thing is fine).
    When you visit a category Category C and go to the previous/next section you should only see the links within Category C.

    Please let me know if I’m not clear

    #899978
    David
    Staff
    Customer Support
    #899982
    Elia

    Thanks David,

    The Tom’s code is to isolate each category from each other. In our case we need to isolate just 1 category from others

    #900005
    David
    Staff
    Customer Support

    I think this may require some custom development, so before we look at to see if we can help lets make sure i fully understand ๐Ÿ™‚

    Posts within Category C
    Post Navigation to be limited to posts within Category C only.

    Posts not within Category C
    Post Navigation to work as per default but to exclude Posts from Category C.

    Is that correct?

    #900008
    Elia

    Yes, David. Thanks a lot!

    #900389
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    I’m not 100% sure this will work, but it’s worth a shot:

    add_filter( 'generate_category_post_navigation', function( $in_same_term ) {
        if ( has_category( 'Category C' ) ) {
            $in_same_term = true;
        }
    
        return $in_same_term;
    } );
    
    add_filter( 'get_next_post_excluded_terms', 'tu_filter_excluded_terms' );
    add_filter( 'get_previous_post_excluded_terms', 'tu_filter_excluded_terms' );
    function tu_filter_excluded_terms( $excluded_terms ) {
        if ( ! has_category( 'Category C' ) ) {
            $excluded_terms[] = 10; // The ID of Category C.
        }
    
        return $excluded_terms;
    }
    #901245
    Elia

    Hi Tom,

    thanks a lot!

    For some reason the code doesn’t work. Could it be because my posts belongs not to category itself but to subcategories?

    #902049
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Did neither function work? The second one is somewhat experimental, but the first one should work if the has_category() condition is correct.

    Let me know ๐Ÿ™‚

    #912909
    Elia

    Hi Tom,

    Do I need to just change the category ID in your code, right?

    If yes, then the code doesn’t work for some reason

    #913451
    Leo
    Staff
    Customer Support

    I think you will need to change Category C and 10

    #913459
    Elia

    Thanks Leo, do I have to change the “Category C” to the exact name of the category? I tried that it doesn’t work

    #913670
    Tom
    Lead Developer
    Lead Developer

    Can you share the function you’re using?

    Also, can you link me to the page where it should be working?

    Thanks!

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