[Resolved] Next / Previous Links in Blog Posts

Home Forums Support Next / Previous Links in Blog Posts

Viewing 15 posts - 16 through 30 (of 31 total)
  • Author
    Posts
  • #1010837
    Jessica

    The previous/next post titles are showing up at the “title” of the post automatically so the code you pasted is likely interfering with the inherent code. Try removing that code. What happened before you added that code?

    #1010850
    litesprint

    But then it would not serve my purpose of modifying the “visible” link text (from postname to previous post).

    #1010857
    Jessica

    Hrm. I didn’t have to add code to make the post title show up (I don’t think) so I’m not sure why you’re having the issue. You mentioned “modifying the visible link text.

    What exactly do you want it to say? I was under the impression fro your earlier comment you wanted it to show the post title, not previous or next. Is that correct?

    #1010859
    litesprint

    <a href="link" title="Post Title">Previous post</a>

    This is what I want.

    #1010861
    Jessica

    Are you editing the “single.php” template? If so, the only reason to do that is if you’re adding other elements (ie: I added a newsletter signup box). Otherwise, no additional code is needed at the post titles show up automatically.

    #1010862
    Jessica

    Ah, you said in your original comment you wanted it to be the post title and NOT the “previous or next post” text.

    #1010863
    litesprint

    No, I’m not editing any theme file. I’m using the code inserter plugin. And it’s working fine (code by Tom on Github), but messing the title attribute too, which I don’t want.

    #1010865
    Jessica

    In this case, you shouldn’t have to insert code to create links to your previous and next posts.

    What happens when you remove that code you inserted with the plugin? Are there any links showing at all? Can you share a link to the page(s) your talking about? That might help better understand.

    #1010872
    litesprint

    You’re not at all understanding what I’m trying to accomplish, Jessica.

    I know the theme already has the option to display links to previous and next posts showing their full post names. But I do NOT want that exactly that way. Which is why I added that code to change the linking text. But it’s affecting the ‘title’ attribute (which is DIFFERENT from the linking text), which I don’t want.

    #1010875
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    Give this a shot:

    <?php
    previous_post_link(
        '<div class="nav-previous"><span class="prev" title="%link">' . esc_attr__( 'Previous', 'generatepress' ) . '</span></div>',
        '%title'
    );
    
    next_post_link(
        '<div class="nav-next"><span class="next" title="%link">' . esc_attr__( 'Next', 'generatepress' ) . '</span></div>', 
        '%title',
    );
    ?>

    Let me know 🙂

    #1010882
    litesprint

    Not working.

    It’s resulting in:

    < Post Title”>Previous

    Here’s the entire code I’m using by the way. And I’m only looking to modify the link text on single posts, not pagination or anything else.

    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 previous_post_link( '<div class="nav-previous"><span class="prev" title="' . __('Previous post','generate') . '">%link</span></div>', __('Previous post','generate') ); ?>
    		<?php next_post_link( '<div class="nav-next"><span class="next" title="' . __('Next post','generate') . '">%link</span></div>', __('Next post','generate') ); ?>
    
    	<?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
    #1010920
    Tom
    Lead Developer
    Lead Developer

    Try this instead:

    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
    #1010923
    litesprint

    Works perfectly! Thanks a ton, Tom!

    By the way, are the surrounding codes also necessary if I only care about it on single posts? If not, I’d prefer a shorter and cleaner code just for single pages.

    For example:

    // navigation links for home, archive, and search pages
    and
    // Don’t print empty markup in archives if there’s only one page.

    #1010924
    Tom
    Lead Developer
    Lead Developer

    Those are just comments – you can keep them or remove them, it won’t make a difference.

    At some point, you should explore using filters to make changes instead of overwriting the entire function like that. Overwriting the entire function means you won’t get any updates we make to it in future versions.

    #1010926
    litesprint

    I wasn’t referring to the comments themselves, but the respective sections of code.

    Also, I don’t understand PHP, and that bit of code was taken from your Github which you’d suggested in an older thread. I’m always eager to get the job done using the most efficient way.

Viewing 15 posts - 16 through 30 (of 31 total)
  • You must be logged in to reply to this topic.