Skip to content

Instantly share code, notes, and snippets.

@stephenscaff
Created December 18, 2014 16:13
Show Gist options
  • Select an option

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

Select an option

Save stephenscaff/e1ec101c7973b19faec6 to your computer and use it in GitHub Desktop.
Get next and last post in Wordpress, using first image from post (catch_that_image) instead of a featured image, styled as background-image for container-less half grid.
<!-- Single Next/Last Post nav
================================================== -->
<section class="sect-posts post-nav cf">
<?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);
?>
<article class="g-6 cols" style="background-image:url('<?php echo catch_that_image($page->ID, 'fullsize') ?>');">
<a href="<? the_permalink()?>" style="background-image:url('<?php echo catch_that_image($page->ID, 'fullsize') ?>');">
<div class="content">
<!--<small class="meta-date"><?php the_time('F j, Y'); ?></small>-->
<small class="meta-date">Last Post</small>
<hr class="sep"/>
<h3><?php the_title_shorten(45,'...'); ?></h3>
</div>
<div class="overlay"></div>
</a>
</article>
<?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);
?>
<article class="g-6 cols" style="background-image:url('<?php echo catch_that_image() ?>');">
<a href="<? the_permalink()?>" style="background-image:url('<?php echo catch_that_image() ?>');">
<div class="content">
<!--<small class="meta-date"><?php the_time('F j, Y'); ?></small>-->
<small class="meta-date">Next Post</small>
<hr class="sep"/>
<h3><?php the_title_shorten(45,'...'); ?></h3>
</div>
<div class="overlay"></div>
</a>
</article>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</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