The default WordPress setup on a post page is to link to the previous and next posts with the “previous” and “next” buttons. But what if we want a more elegant solution, showing the titles of the previous and next posts instead?
Here is a simple solution which you are welcome to customize to your needs:
Edit your theme’s “functions.php” file (located under the /wp-content/themes/”your-chosen-theme” folder) and add at the bottom the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Add Title to next post link function my_next_post_link($output, $format, $link, $post, $adjacent) { $next_post = get_adjacent_post(false, '', false); if(!empty($next_post)) { return 'Read about: <a href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">' . $next_post->post_title . '</a>'; } else return ''; } add_filter( 'next_post_link', 'my_next_post_link', 10, 5 ); // Add Title to previous post link function my_previous_post_link($output, $format, $link, $post, $adjacent) { $prev_post = get_adjacent_post(false, '', true); if(!empty($prev_post)) { return 'Read about: <a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; } else return ''; } add_filter( 'previous_post_link', 'my_previous_post_link', 10, 5 ); |
You can customize the return strings in each function to your preferred phrasing. I used “Read about: post-title“.
Comment if this helped you or if additional help is required.