Home › Forums › Support › Post Navigation – How exclude a category from "next" and "previous" post links?
- This topic has 26 replies, 2 voices, and was last updated 1 year, 10 months ago by Fernando.
-
AuthorPosts
-
January 18, 2017 at 6:41 am #265393Sebastian
Hi Tom,
How exclude a category from “next” and “previous” post links?
Regards, Sebastian
January 18, 2017 at 6:49 am #265399LeoStaffCustomer SupportHi Sebastian,
Is this what you have in mind? https://generatepress.com/forums/topic/exclude-category-from-loop/#post-111543
Let us know.
January 18, 2017 at 7:05 am #265408SebastianHi 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.
January 18, 2017 at 10:06 am #265471TomLead DeveloperLead DeveloperHmm, 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
January 18, 2017 at 11:30 pm #265669SebastianThank you very much. I’ll try it
January 19, 2017 at 1:39 pm #265907SebastianGreat, it works. Thanks.
I just added
$excluded_terms = '1'
(1 = Category ID) to previous_post_link and next_post_link in template-tags.phpJanuary 19, 2017 at 4:58 pm #265946TomLead DeveloperLead DeveloperGlad you got it working 🙂
February 4, 2017 at 9:50 am #274185SebastianHi Tom, is there another way? It is tedious to update the lines in template-tags.php after each theme update.
February 4, 2017 at 12:23 pm #274250TomLead DeveloperLead DeveloperYou can copy the entire function (including ! function_exists()) and add it to your child theme/functions file.
It will overwrite the parent function.
January 27, 2018 at 10:54 pm #481446Feriso 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
January 28, 2018 at 10:20 am #481862TomLead DeveloperLead DeveloperLooked 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.
January 28, 2018 at 3:36 pm #482005Feriactually 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 );
January 28, 2018 at 7:04 pm #482078TomLead DeveloperLead DeveloperYou 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.
January 28, 2018 at 7:35 pm #482082Feriadd_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
January 28, 2018 at 7:36 pm #482083TomLead DeveloperLead DeveloperCan you link me to the category?
-
AuthorPosts
- You must be logged in to reply to this topic.