Home › Forums › Support › Simplifying Code for Custom Text for 'Previous/Next Post' Links in Single Posts
- This topic has 5 replies, 2 voices, and was last updated 3 years, 2 months ago by David.
-
AuthorPosts
-
September 16, 2019 at 11:29 am #1010934litesprint
Existing code:
if ( ! function_exists( 'generate_content_nav' ) ) : /** * Display navigation to next/previous pages when applicable */ function generate_content_nav( $nav_id ) { 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'; ?> <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>"> <h6 class="screen-reader-text"><?php _e( 'Post navigation', 'generate' ); ?></h6> <?php if ( is_single() ) : // navigation links for single posts ?> <?php $prev_post = get_adjacent_post( false, '', true ); previous_post_link( '<div class="nav-previous"><span class="prev" title="' . esc_attr( $prev_post->post_title ) . '">%link</span></div>', esc_attr__( 'Previous', 'generatepress' ) ); $next_post = get_adjacent_post( false, '', false ); next_post_link( '<div class="nav-next"><span class="next" title="' . esc_attr( $next_post->post_title ) . '">%link</span></div>', esc_attr__( 'Next', 'generatepress' ) ); ?> <?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?> <?php if ( get_next_posts_link() ) : ?> <div class="nav-previous"><span class="prev" title="<?php _e('Previous','generate');?>"><?php next_posts_link( __( 'Older posts', 'generate' ) ); ?></span></div> <?php endif; ?> <?php if ( get_previous_posts_link() ) : ?> <div class="nav-next"><span class="next" title="<?php _e('Next','generate');?>"><?php previous_posts_link( __( 'Newer posts', 'generate' ) ); ?></span></div> <?php endif; ?> <?php generate_paging_nav(); ?> <?php do_action('generate_paging_navigation'); ?> <?php endif; ?> </nav><!-- #<?php echo esc_html( $nav_id ); ?> --> <?php } endif; // generate_content_nav
Goal: changing linking texts to “Previous post” and “Next post” from “Post Title”, without affecting the HTML “title” attribute of the ‘a’ tag (should still have title=”Post Title”).
Can this be simplified using filters instead of overwriting the whole function (as suggested by you)?
September 16, 2019 at 5:08 pm #1011119TomLead DeveloperLead DeveloperHi there,
So instead of overwriting that function, we should be able to filter into the core WP code like this:
add_filter( 'next_post_link', function( $output, $format, $link, $post, $adjacent ) { $next_post = get_adjacent_post( false, '', false ); return sprintf( '<div class="nav-next"><span class="next" title="%1$s"><a href="%3$s">%2$s</a></span></div>', esc_attr( $next_post->post_title ), __( 'Next' ), get_permalink( $next_post->ID ) ); }, 10, 5 ); add_filter( 'previous_post_link', function( $output, $format, $link, $post, $adjacent ) { $prev_post = get_adjacent_post( false, '', true ); return sprintf( '<div class="nav-previous"><span class="prev" title="%1$s"><a href="%3$s">%2$s</a></span></div>', esc_attr( $prev_post->post_title ), __( 'Previous' ), get_permalink( $prev_post->ID ) ); }, 10, 5 );
Let me know ๐
September 17, 2019 at 12:12 am #1011244litesprintWorked perfectly! Thanks again!
September 17, 2019 at 10:28 am #1011825TomLead DeveloperLead DeveloperYou’re welcome ๐
December 31, 2020 at 1:19 pm #1601078KevinBased on this Tom code, how can I change the text written in Next and Prev? I tried but I didn’t succeed … I want a function for that.
January 1, 2021 at 5:56 am #1601469DavidStaffCustomer SupportHi there,
if you want to simply to chenge the next prev labels – then you simply change the strings in these two lines:
__( 'Next' ),
and
__( 'Previous' ),
-
AuthorPosts
- You must be logged in to reply to this topic.