[Resolved] Simplifying Code for Custom Text for 'Previous/Next Post' Links in Single Posts

Home Forums Support [Resolved] Simplifying Code for Custom Text for 'Previous/Next Post' Links in Single Posts

Home Forums Support Simplifying Code for Custom Text for 'Previous/Next Post' Links in Single Posts

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1010934
    litesprint

    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)?

    #1011119
    Tom
    Lead Developer
    Lead Developer

    Hi 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 ๐Ÿ™‚

    #1011244
    litesprint

    Worked perfectly! Thanks again!

    #1011825
    Tom
    Lead Developer
    Lead Developer

    You’re welcome ๐Ÿ™‚

    #1601078
    Kevin

    Based 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.

    #1601469
    David
    Staff
    Customer Support

    Hi 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' ),

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