18 Dec 2010

WordPress: Better previous and next post links

To be used on single post, the snippet below will not output unnecessary markup if there’s:

  1. only 1 published post.
  2. no post that is adjacent (previous or next) to current post.
<?php php if ( get_adjacent_post( false, '', false ) ||  get_adjacent_post( false, '', true ) ) : ?>
  <div class="navigation">
    <?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">&larr;</span> %title' ); ?>
    <?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">&rarr;</span>' ); ?>
  </div>
<?php endif; ?>