- This topic has 30 replies, 6 voices, and was last updated 5 years, 7 months ago by
Tom.
-
AuthorPosts
-
September 16, 2019 at 9:44 am #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?
September 16, 2019 at 9:53 am #1010850litesprint
But then it would not serve my purpose of modifying the “visible” link text (from postname to previous post).
September 16, 2019 at 9:58 am #1010857Jessica
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?
September 16, 2019 at 10:00 am #1010859litesprint
<a href="link" title="Post Title">Previous post</a>
This is what I want.
September 16, 2019 at 10:00 am #1010861Jessica
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.
September 16, 2019 at 10:01 am #1010862Jessica
Ah, you said in your original comment you wanted it to be the post title and NOT the “previous or next post” text.
September 16, 2019 at 10:02 am #1010863litesprint
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.
September 16, 2019 at 10:06 am #1010865Jessica
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.
September 16, 2019 at 10:09 am #1010872litesprint
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.
September 16, 2019 at 10:13 am #1010875Tom
Lead DeveloperLead DeveloperHi 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 🙂
September 16, 2019 at 10:21 am #1010882litesprint
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
September 16, 2019 at 11:10 am #1010920Tom
Lead DeveloperLead DeveloperTry 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
September 16, 2019 at 11:15 am #1010923litesprint
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.September 16, 2019 at 11:20 am #1010924Tom
Lead DeveloperLead DeveloperThose 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.
September 16, 2019 at 11:22 am #1010926litesprint
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.
-
AuthorPosts
- You must be logged in to reply to this topic.