Want to get next/previous post hrefs and titles in WordPress? No need for functions and filters all you need to do is to use get_adjacent_post() instead of next_post_link() and prev_post_link(), Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it’s title attribute use this:
1 2 3 4 | $prev_post = get_adjacent_post(false, '', true); if(!empty($prev_post)) { echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; } |
To get next post and it’s title attribute use this:
1 2 3 4 | $next_post = get_adjacent_post(false, '', false); if(!empty($next_post)) { echo '<a href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">' . $next_post->post_title . '</a>'; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.