Wordpress

How to use Thumbnails with Previous and Next Post Links in WordPress

One of our users asked us on our Facebook page: How to use thumbnails for previous/next post navigation in WordPress. The next_post_link and previous_post_link functions doesn’t have a simple enough thumbnail parameter which a new developer can simply turn on and off. In this article, we will show you how to use post thumbnails with previous and next post links in WordPress.

The final result would look like this:

Use Thumbnails with Previous and Next Post links in WordPress

First thing you need to do is open your theme’s single.php file and add the following code inside the loop most likely after the_content() area:

<div id="cooler-nav" class="navigation">

02 <?php $prevPost = get_previous_post(true);
03 if($prevPost) {?>
04 <div class="nav-box previous">
05 <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );?>
06 <?php previous_post_link('%link',"$prevthumbnail  <p>%title</p>", TRUE); ?>
07 </div>
08
09 <?php } $nextPost = get_next_post(true);
10 if($nextPost) { ?>
11 <div class="nav-box next" style="float:right;">
12 <?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); } ?>
13 <?php next_post_link('%link',"$nextthumbnail  <p>%title</p>", TRUE); ?>
14 </div>
15 <?php } ?>
16 </div><!--#cooler-nav div -->

Next thing you need to do is open your style.css file and add the following styles:

#cooler-nav{clear: both; height: 100px; margin: 0 0 70px;}

2 #cooler-nav .nav-box{background: #e9e9e9; padding: 10px;}
3 #cooler-nav img{float: left; margin: 0 10px 0 0;}
4 #cooler-nav p{margin: 0 10px; font-size: 12px; vertical-align: middle;}
5 #cooler-nav .previous{float: left; vertical-align: middle; width: 250px; height: 100px;}
6 #cooler-nav .next{float: right; width: 250px;}

Feel free to change the styling to match it with your theme. Often our users like to style the code, so it makes it easier for them to tweak it. This is just basic styling which you should be able to customize easily.

If you want to change the thumbnail size, then simply change the array(100,100) to whatever you like.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s