How to Add Image Thumbnails to Related Posts in WordPress

Q: Your site is very helpful to me. Could you please tell me how to embed that related posts like in your site in mine? I have tried a lot of alternatives, can you please tell me how you add that related posts with thumbnails?

If you are frequently browsing the Web, you will notice that many blogs are showing image thumbnails for the Related Posts section, just like what we did in Make Tech Easier. In WordPress, there are several ways that this can be done. Let’s check it out.

Using a plugin

The easiest way to implement the image thumbnail (without any coding) is via a plugin. LinkWithin is one such tool that you can use. Advantage of LinkWithin is that it doesn’t take up any of your server space and bandwidth (to store the thumbnails) and there are little (or zero) configurations need to be done.

Go to LinkWithin. Fill in the detail on the front page.

wpthumb-linkwithin

Click Get Widget. You will then be directed to a page where you can download the plugin.

Install and activate the plugin. Your thumbnail related post will immediately appear in your blog.

wpthumb-linkwithin-on-post

As good as it can get, there are some disadvantages and limitation of using LinkWithin.

  1. there is limited control for the end-user: you are not able to configure which image thumbnail to show for each post.
  2. you cannot adjust the size and styling of the widget.
  3. you cannot set the position for the widget to appear. The default is for it to appear immediately after the post content ends, but there are times where you want to insert author information or advertisements first before the related post. in this case, this doesn’t come handy.

Hacking the WP theme file

The best way to fully customize the user experience is to go into the theme file and do some hacking. This will require some coding experience.

First, using the instruction here, activate the post thumbnail feature (only applicable in WP 2.9 and above).

Next, install and activate the Yet Another Related Posts plugin. What I love about this is the ability to use a template system to style up the related post content.

In the YARPP plugin folder, go to the yarpp-templates folder and copy the file “yarpp-template-thumbnail.php” to your theme folder.

Next in the WordPress YARPP options page:

  • uncheck the “Automatically displayed related posts?
  • select the number of related posts that you want to display
  • choose the yarpp-template-thumbnail.php from the template file dropdown list.

wpthumb-yarpp-options

Open the “yarpp-template-thumbnail.php” in a text editor. Replace the code with the following.

<div class="related_posts">
<h3>Related Photos</h3>
<?php if ($related_query->have_posts()):?>
<ul>
    <?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
        <?php if (function_exists('has_post_thumbnail')): if (has_post_thumbnail()):?>
	<li>
	    <div class="related_thumbnail">
	        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
	    </div>
	    <div style="clear:both;"></div>
	    <div class="related_permalink">
	        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>		
	    </div>
	</li>
	<?php endif; endif; ?>
    <?php endwhile; ?>
</ul>
 
<?php else: ?>
    <p>No related photos.</p>
<?php endif; ?>
</div>

What this code does is to check if there is a thumbnail set for each related post. If yes, it will display the thumbnail and the permalink.

Next, in your theme folder, open your “single.php” file and add the following line to the part where you want to display the related post:

<?php if(function_exists('related_posts')) related_posts(); ?>

Lastly, we need to do some styling. Copy and paste the following styling code to your style.css file.

.related_posts ul{ width:100%; list-style:none; margin:0; padding:0; }.related_posts li { display:inline; float:left; margin:0; padding-left:0; padding-right:10px; width:140px; /* change this value accordingly to your template */ }.related_permalink{ width:140px;/*change this value accordingly to your template */ text-align:left; font-size:0.85em; padding-top:5px; }.related_thumbnail{ width:140px; /*change this value accordingly to your thumbnail size */ height:80px; overflow:hidden; text-align:center; }

That's it.

Image credit: wuzhao.album