Quick Tip: Next and Previous Posts With Thumbnails

Quick Tip: Next and Previous Posts With Thumbnails

Media rich content is engaging content, right? And giving readers an opportunity to browse through posts linearly is one good way to keep them at your site reading longer, right? So why just give them a title? This quick tip will give you the programming you need to include the post thumbnail and the date of the next and previous posts. Styling is left up to you.


Check for the Previous and Next Posts

$prevPost = get_previous_post(true);
$nextPost = get_next_post(true);

These two variables will get the previous and next posts if they exist. Now we can check to see if they exist and use the ID with get_posts() to display any information we want to about each post.


Output the Thumbnails and Other Stuff

<div id="post-nav">
	<?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="post-previous">
			<a class="previous" href="<?php the_permalink(); ?>">&laquo; Previous Story</a>
			<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
			<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
			<small><?php the_date('F j, Y'); ?></small>
		</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="post-next">
			<a class="next" href="<?php the_permalink(); ?>">Next Story &raquo;</a>
			<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
			<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
			<small><?php the_date('F j, Y'); ?></strong>
		</div>
	<?php
				wp_reset_postdata();
			} //end foreach
		} // end if
	?>
</div>

This code will go into your single.php template. If the posts exist, we use the ID to get that one post and then create a foreach loop to output the following:

  • A generically labeled "Previous/Next" link
  • The post thumbnail wrapped in a link to the post
  • The title in an h2 and wrapped in a link to the post
  • and the date

Conclusion

Once you have this set up with the styling that you prefer you can do any number of things with the post information. You might have something that looks like this:

Custom Meta Box
Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.ruturaaj.com Ruturaaj

    Hey… this is nice; I will surely implement this in my current theme project. Just wish to submit that it’s ok to use the the_post_thumbnail if you’re developing theme for yourself; but if the theme is for a client then it’s good to use it with has_post_thumbnail. Check if User has specified any post thumbnail, if yes use it; if no then either use a default image or just apply different CSS so the presentation remains good. For the kind of purpose demonstrated here in this post, I would prefer to use some default image.

    • http://www.ruturaaj.com Ruturaaj

      I also feel this sort of Previous and Next link with title of Post set as ALT attribute to IMG and Title of A tag may add a good value to SEO of site. Just a thought…

  • Pingback: Quick Tip: Next and Previous Posts With Thumbnails | Qtiva

  • http://freakify.com Ahmad Awais

    Nice tutorial.Was a great resource of help.

  • http://bloginners.co.uk James Watts

    Nice and simple tutorial, but it’s these type of simple tweaks that can make your site really stand out!

  • Pingback: Next and Previous Posts With Thumbnails in WordPress « PRASATH PREE

  • http://www.wpsquare.com/ Bharat Chowdary

    Thanks for the code snippet, I’m looking for the one. Will add this feature to my blog.

  • http://www.customicondesign.com CUSTOM ICON DESIGN

    it should useful to implement. Very simple but useful. many thanks.

  • http://bucurion.info/ Bucur

    very vey good job,bravo…

  • http://www.wpexplorer.com AJ Clarke

    sweet tutorial. thanks ;)

  • Pingback: Tweet-Parade (no.23 June 2012) | gonzoblog.nl

  • http://www.elliot-thrash.com/ Anton

    Thank you!!! It’s just what I need. But there is a small mistake there. There is an incorrect closing tag , but it should be there. Please correct this small mistake please.

  • http://elliot-thrash.com Anton

    There is incorrect closing tag in row number 37. There is “strong”, but “small” should be there

  • http://www.amrabdelaziz.com amrabdelaziz

    great tuts
    thnx so much O,o

  • http://320volt.com 320volt

    Nice and simple tip thanks @Tammy Hart

    my mod. add custom field image :)

    http://pastebin.com/6ivY30JU

  • Pingback: Die besten meiner geteilten Links auf Twitter im Juni 2012 - pixelstrol.ch

  • http://codzine.com Saurabh

    Use this:

    ID );
    previous_post_link( ‘%link’, $prevThumbnail );
    ?>

  • http://www.geekandblogger.com Pavan Somu

    Won’t it shows the excerpts.

  • Matt

    Does anyone have the CSS used to create the example above, or something similar?

  • Adriana

    Great tip! I’ve been looking for a solution for some time. Instead of using the thumbnail, I’ve replaced it with catch that image script. Thanks again!

  • http://blogcheer.com Umer Rock

    really nice article i used next and previous post features in my blog and learned alot stuff from here

  • lostartskate

    Hi, thanks for the tutorial. I am just wondering how do I resize the thumbnail size? Cheers