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, 9 months ago by Fernando.
-
AuthorPosts
-
January 28, 2018 at 7:47 pm #482090Feri
https:// . com/ global/
and i also have Ultimate Category Excluder plugin installed, i dont know maybe cause conflict or…
January 28, 2018 at 8:40 pm #482120Ferior maybe i can send you a temporary admin login?
January 28, 2018 at 11:49 pm #482183TomLead DeveloperLead DeveloperPerhaps try this instead:
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' ); }
January 28, 2018 at 11:57 pm #482186Ferinothing happen, i dont know what is wrong with the code
January 29, 2018 at 5:06 pm #483084FeriTry to clear cache, browser history, refresh etc and link post (exclude category) still showing on next/previous link
January 29, 2018 at 7:13 pm #483128TomLead DeveloperLead DeveloperWell that was harder than it should have been. Looks like a bug in WP where the filter doesn’t work if no excluded terms are added by default.
Instead, add this function: https://gist.github.com/generatepress/803a950d6381dd98e6bc4418b817ed82
January 29, 2018 at 8:22 pm #483147Feriwow it works
thank you tom for your great support and answerJanuary 29, 2018 at 8:43 pm #483161TomLead DeveloperLead DeveloperGlad I could help! 🙂
June 8, 2022 at 5:57 pm #2247785DMHi Tom! Your function still works, BUT it removed the forward “<” and “>” back arrows.
June 8, 2022 at 6:24 pm #2247795Fernando Customer SupportHi DM,
Can you try this instead? Added the arrows in the code by Tom:
if ( ! function_exists( 'generate_content_nav' ) ) { /** * Display navigation to next/previous pages when applicable. * * @since 0.1 * * @param string $nav_id The id of our navigation. */ function generate_content_nav( $nav_id ) { if ( ! apply_filters( 'generate_show_post_navigation', true ) ) { return; } global $wp_query, $post; // Don't print empty markup on single pages if there's nowhere to navigate. if ( is_single() ) { $previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true ); $next = get_adjacent_post( false, '', false ); if ( ! $next && ! $previous ) { return; } } // Don't print empty markup in archives if there's only one page. if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) ) { return; } $nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation'; $category_specific = apply_filters( 'generate_category_post_navigation', false ); ?> <nav id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo esc_attr( $nav_class ); ?>"> <span class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'generatepress' ); ?></span> <?php if ( is_single() ) : // navigation links for single posts. previous_post_link( '<div class="nav-previous">' . generate_get_svg_icon( 'arrow-left' ) . '<span class="prev" title="' . esc_attr__( 'Previous', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific, array( '29' ) ); next_post_link( '<div class="nav-next">' . generate_get_svg_icon( 'arrow-right' ) . '<span class="next" title="' . esc_attr__( 'Next', 'generatepress' ) . '">%link</span></div>', '%title', $category_specific, array( '29' ) ); 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; if ( function_exists( 'the_posts_pagination' ) ) { the_posts_pagination( array( 'mid_size' => apply_filters( 'generate_pagination_mid_size', 1 ), 'prev_text' => apply_filters( 'generate_previous_link_text', __( '← Previous', 'generatepress' ) ), 'next_text' => apply_filters( 'generate_next_link_text', __( 'Next →', 'generatepress' ) ), ) ); } /** * generate_paging_navigation hook. * * @since 0.1 */ do_action( 'generate_paging_navigation' ); endif; ?> </nav><!-- #<?php echo esc_html( $nav_id ); ?> --> <?php } }
Kindly let us know how it goes.
June 8, 2022 at 6:52 pm #2247807DMHi there Fernando!
Thanks for your help! It works! Just replaced the ” ’29’ ” with my category IDs. 🙂June 8, 2022 at 6:54 pm #2247810Fernando Customer SupportYes, forgot to mention that. You’re welcome DM! 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.