[Resolved] Post Navigation – How exclude a category from "next" and "previous" post links?

Home Forums Support [Resolved] Post Navigation – How exclude a category from "next" and "previous" post links?

Home Forums Support Post Navigation – How exclude a category from "next" and "previous" post links?

Viewing 15 posts - 1 through 15 (of 27 total)
  • Author
    Posts
  • #265393
    Sebastian

    Hi Tom,

    How exclude a category from “next” and “previous” post links?

    Regards, Sebastian

    #265399
    Leo
    Staff
    Customer Support

    Hi Sebastian,

    Is this what you have in mind? https://generatepress.com/forums/topic/exclude-category-from-loop/#post-111543

    Let us know.

    #265408
    Sebastian

    Hi Leo,

    Thank you for your quick response. No, that’s not what I mean. I try to explain it differently.

    In a post content you can see the “post navigation”. (.entry-meta -> .nav-previous and .nav-next). How I would like to exclude a category from this navigation. I hope you understand what i mean.

    #265471
    Tom
    Lead Developer
    Lead Developer

    Hmm, it looks like you would need to overwrite the entire function and then follow what they did here: http://wordpress.stackexchange.com/questions/176146/excluding-a-category-from-next-and-previous-post-links

    This is the function you would need to overwrite: https://github.com/tomusborne/generatepress/blob/2.0.2/inc/structure/post-meta.php#L12-L82

    #265669
    Sebastian

    Thank you very much. I’ll try it

    #265907
    Sebastian

    Great, it works. Thanks.

    I just added $excluded_terms = '1' (1 = Category ID) to previous_post_link and next_post_link in template-tags.php

    #265946
    Tom
    Lead Developer
    Lead Developer

    Glad you got it working 🙂

    #274185
    Sebastian

    Hi Tom, is there another way? It is tedious to update the lines in template-tags.php after each theme update.

    #274250
    Tom
    Lead Developer
    Lead Developer

    You can copy the entire function (including ! function_exists()) and add it to your child theme/functions file.

    It will overwrite the parent function.

    #481446
    Feri

    so the template tags move to inc/structure/post-meta, right?

    and i dont know how to add that code. i found this code…

    <?php if ( is_single() ) : // navigation links for single posts.
                    
    				previous_post_link( '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific );
    				next_post_link( '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific );
    
    			elseif ( is_home() || is_archive() || is_search() ) : // navigation links for home, archive, and search pages.
    
    				if ( get_next_posts_link() ) : ?>
    					<div class="nav-previous"><span class="prev" title="<?php esc_attr_e( 'Previous', 'generatepress' );?>"><?php next_posts_link ( __( 'Older posts', 'generatepress' ) ); ?></span></div>
    				<?php endif;
    
    				if ( get_previous_posts_link() ) : ?>
    					<div class="nav-next"><span class="next" title="<?php esc_attr_e( 'Next', 'generatepress' );?>"><?php previous_posts_link ( __( 'Newer posts', 'generatepress' ) ); ?></span></div>
    				<?php endif;

    can you help how to add $excluded_terms = '1' more detail.

    thank you very much

    #481862
    Tom
    Lead Developer
    Lead Developer

    Looked through some core WP code and found a filter we can use instead.

    For example:

    add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
    add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );
    function tu_exclude_terms() {
        return array( 10, 15 );
    }

    10 and 15 being the IDs of the terms you wish to exclude.

    #482005
    Feri

    actually i dont understand how to implement that code. something like this?

    <?php if ( is_single() ) : // navigation links for single posts.
    
    add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
    add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );
    function tu_exclude_terms() {
        return array( 10, 15 );
    }
    				previous_post_link( '<div class="nav-previous"><span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific );
    				next_post_link( '<div class="nav-next"><span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific );
    #482078
    Tom
    Lead Developer
    Lead Developer

    You would just add the code I shared using one of these methods: https://docs.generatepress.com/article/adding-php/

    Then you would need to upload the 10 and 15 values to the actual IDs of the terms you wish to exclude.

    #482082
    Feri
    add_filter( 'get_next_post_excluded_terms', 'tu_exclude_terms' );
    add_filter( 'get_previous_post_excluded_terms', 'tu_exclude_terms' );
    function tu_exclude_terms() {
        return array(3453);
    }

    i add the code to snippet plugin and nothing happen, post under category 3453 still showing at next/previous pagination

    #482083
    Tom
    Lead Developer
    Lead Developer

    Can you link me to the category?

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