Previous and Next Post Links

Looking for a quick way to show your Next and Previous “Posts” in your single.php WordPress site? There’s this great little function that allows you to do just that. I’ve added a title and icons via FontAwesome (you will have to call the FontAwesome CSS for that to work).

get_adjacent_post code

<nav class="navigation post-navigation" role="navigation">
<div class="nav-links">
$p = get_adjacent_post(false, '', true);
if(!empty($p)) echo '<div class="nav-previous"><p class="meta-nav">'.__('Previous','textdomain').'</p><a href="' . get_permalink($p->ID) . '" title="' . $p->post_title . '"><i class="fa fa-chevron-circle-left"></i> ' . $p->post_title . '</a></div>';

$n = get_adjacent_post(false, '', false);
if(!empty($n)) echo '<div class="nav-next"><p class="meta-nav">'.__('Next','textdomain').'</p><a href="' . get_permalink($n->ID) . '" title="' . $n->post_title . '">' . $n->post_title . ' <i class="fa fa-chevron-circle-right"></i></a></div>';
</div><!-- .nav-links -->
</nav><!-- .navigation -->

So just drop the above code into your single.php file below your Posts.

You will have to change the ‘textdomain’ to the phrase you use for your themes translation purpose.

WordPress reference on get_adjacent_post.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.