Skip to content

Instantly share code, notes, and snippets.

@stephenscaff
Created October 7, 2014 15:41
Show Gist options
  • Select an option

  • Save stephenscaff/291391ee46e3f989f543 to your computer and use it in GitHub Desktop.

Select an option

Save stephenscaff/291391ee46e3f989f543 to your computer and use it in GitHub Desktop.
Snags next and previous posts in Wordpress. Intended for a single.php template. Can also pull images with a <?php the_post_thumbnail('thumbnail'); ?> or a catch first img function.
<!-- Single Next/Last Post nav
================================================== -->
<section class="post-nav">
<div class="row">
<?php $prevPost = get_previous_post(true);
if($prevPost) {
$args = array(
'posts_per_page' => 1,
'include' => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<div class="col">
<a class="link-page" href="<?php the_permalink(); ?>">
<span class="icon icon-left-caret"></span>
<time><?php the_date('F j, Y'); ?></time>
<h4><?php the_title(); ?></h4>
<p><?php echo get_excerpt(); ?></p>
</a>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
$nextPost = get_next_post(true);
if($nextPost) {
$args = array(
'posts_per_page' => 1,
'include' => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<div class="col">
<a class="link-page" href="<?php the_permalink(); ?>">
<span class="icon icon-right-caret"></span>
<time><?php the_date('F j, Y'); ?></time>
<h4><?php the_title(); ?></h4>
<p><?php echo get_excerpt(); ?></p>
</a>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</div>
</section><!-- .navigation -->
<?php endwhile; // End the loop ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment