Archived topic
Post Navigation - How exclude a category from "next" and "previous" post links?
26 replies · Started by Sebastian on January 18, 2017
Hi Tom,
How exclude a category from "next" and "previous" post links?
Regards, Sebastian
Hi Sebastian,
Is this what you have in mind? https://generatepress.com/forums/topic/exclude-category-from-loop/#post-111543
Let us know.
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.
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
Thank you very much. I'll try it
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
Glad you got it working :)
Hi Tom, is there another way? It is tedious to update the lines in template-tags.php after each theme update.
You can copy the entire function (including ! function_exists()) and add it to your child theme/functions file.
It will overwrite the parent function.
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
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.
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 );
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.
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
Can you link me to the category?