Skip to content

Instantly share code, notes, and snippets.

@timstl
Last active December 17, 2015 07:09
Show Gist options
  • Save timstl/5570308 to your computer and use it in GitHub Desktop.
Save timstl/5570308 to your computer and use it in GitHub Desktop.
WordPress Fancy Adjacent Post Links. For use on regular posts or custom post type.
function fancy_adjacent_post_link($prev=false, $iter=1)
{
global $post;
$link = $title = '';
$a_post = get_adjacent_post( false, '', $prev );
if ($a_post && $a_post->ID > 0)
{
$link = get_permalink( $a_post->ID );
$title = apply_filters('the_title', get_the_title($a_post->ID));
}
else
{
$order = 'ASC';
if ($prev) { $order = 'DESC'; }
$args = array('post_type' => $post->post_type, 'showposts' => 1, 'order' => $order);
$p_query = new WP_Query($args);
if (!empty($p_query->posts))
{
$link = get_permalink( $p_query->posts[0]->ID );
$title = apply_filters('the_title', $p_query->posts[0]->post_title);
}
wp_reset_postdata();
}
if ($link != '' && $title != '') { fancy_adjacent_html($iter, $prev, $link, $title); }
}
function fancy_adjacent_html($iter, $prev, $link, $title)
{
?>
<li class="col col-2 col-iter-<?php echo $iter; if ($prev) { ?> pagenav-prev<?php } else { ?> pagenav-next<?php } ?>">
<a href="<?php echo $link; ?>">
<span><?php echo $title; ?></span>
</a>
</li>
<?php
}
// example usage:
<nav class="pagenav">
<ul>
<?php fancy_adjacent_post_link(false); ?>
<?php fancy_adjacent_post_link(true, 2); // previous ?>
</ul>
</nav>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment