Aweber - Email Marketing Made Easy
How to Show Related Posts With Thumbnails Without a Plugin in Thesis

How to Show Related Posts With Thumbnails Without a Plugin in Thesis

December 26, 2010 · 53 comments

in Thesis

Thesis 1.x Notice: Thanks for reading this post! So, here's the deal. I wanted you to be aware that this post was written prior to the release of Thesis 2. So that means the instructions or other information provided applies specifically to Thesis 1.x and not the latest 2.x version. Thanks!

Showing related posts at the end of your post is a great way to keep visitors on your site, and make older posts more accessible. You don’t necessarily need a plugin for this and it’s fairly easy to show related posts with a thumbnail without a plugin.

For an example, you can see the related post thumbnails at the end of posts on this blog as I’m using the same method I’m sharing below.

Related Posts With Thumbnails Without a Plugin

Step 1: Add the PHP code

The method used to determine whether a post is related to the one being viewed is through the use of WordPress Post Tags. I’m actually using the code from this tutorial and have modified it only slightly. You can tag similar posts with the same word or phrase and the algorithm will return other posts with any one of the tags that the current post has. I also added a line to display the related posts randomly.

For the thumbnail images, we are using the WordPress Featured Image functionality, so you will need to upload your image using this. The code below includes both the related post code as well as the code necessary to enable and define your thumbnail images.

Paste this into your custom-functions.php file:

// Add Featured Image Support
add_theme_support( 'post-thumbnails' );
add_image_size( 'related-posts', 100, 100, true );

// Related Posts By Tag
function related_posts() {
if ( is_single() ) { 
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1,
'orderby'=>'rand' // Randomize the posts
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts" class="clear"><h3>Related Posts</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<li>
	<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
		<?php the_post_thumbnail( 'related-posts' ); ?>
	</a>
	<div class="related_content">
		<a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
	</div>
</li>
<? }
echo '</ul></div>';
} }
$post = $orig_post;
wp_reset_query(); ?>
<?php } else { } }

add_action('thesis_hook_after_post','related_posts');

You should set your desired thumbnail size over on line #3. If you’re using a WordPress theme other than Thesis, you can still use this code, just insert it in the applicable place in your single.php file. But I’d recommend you use the version that you see on the post I referenced above where I found this code snippet. The Featured Image definitions would need to go in your functions.php file.

Step 2: Add the CSS

Now that you have the code to show your related posts with thumbnails using post tags, you need to style them. I’ve provided some basic styling, but you will need to adjust where needed to work with your blog from a design standpoint.

.custom #related_posts ul {
	margin:0; 
	overflow:hidden; 
}

.custom #related_posts li {
    float: left;
    list-style: none;
    margin: 0 0 0 20px;
}

	.custom #related_posts li:first-child { 
		margin-left:0; 
	}

.custom #related_posts li a {
	display:block;
	font-size:12px;
	line-height:16px;
	text-align:left;
	text-decoration:none;
	width:100px;
}

	.custom #related_posts li a:hover {
		text-decoration:underline;
	}

And Your’re Done!

Let us know how it worked out for you in the comments or if you have questions.

Image attribution: dicktay2000, Larry Johnson, spettacolopuro, treyevan

Was this post helpful? Consider subscribing to my blog via RSS or

This Site Runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced user, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 53 comments… read them below or add one }

SonDan January 18, 2011 at 5:24 pm

I have been looking for something like this since the YARP plugin caused my blog to crash after my most recent Thesis update and I had to uninstall it.

Could the code be adjusted to select pots by specific (not all) Categories and their children? I am not versed in code writing so I don’t know how detailed it can get.

Thanks for reading.

Reply

Thesis-Blogs.com January 19, 2011 at 5:17 pm

Yes, this is definitely doable. Just need to write the code for it. I don’t have anything I can share real quick though. Maybe another reader can provide a possible solution. Alternatively, this plugin might work for you as it appears to work with WP’s Featured Image functionality: http://wordpress.org/extend/plugins/related-posts-by-category

Reply

SonDan April 19, 2011 at 8:54 am

Hi,

I was wondering if any one had tried out this plug-in yet. I am so scared of crashing my blog again until I get a web host site that I feel handles restoration better. When my host restored my blog after using the YAARP plug-in and crashing — I was down for days!

Thanks for reading.
SonDan recently posted..“Eight Days A Week” Performed by The Beatles 1965

Reply

Ahmad Wali February 8, 2011 at 9:32 am

Hello I just copy pasted the given code, but posts are not showing related posts!!
Ahmad Wali recently posted..Earn Money Using Your Skills- Join Online Marketplaces

Reply

Thesis-Blogs.com February 9, 2011 at 6:26 pm

Make sure you have numerous posts with the same tags. This code returns related posts by tag.

Reply

Ahmad Wali February 10, 2011 at 12:30 am

Thanks! Yes worked after putting same tags.
Ahmad Wali recently posted..Earn Money Using Your Skills- Join Online Marketplaces

Reply

Imran@CrazyBloggers February 12, 2011 at 9:11 pm

Is there any possibility that we can use thesis thumbnail instead using featured image

Thanks for Sharing
-Imran
Imran@CrazyBloggers recently posted..Samsung Teases Galaxy S Successor

Reply

Thesis-Blogs.com February 13, 2011 at 6:45 am

Sure. Just don’t use this code then.

Reply

oren February 13, 2011 at 1:30 am

hi.
thank you for this post.
i added tags so it works but i can’t see thumbnails…
maybe you can check it out.. thanks :)

Reply

Thesis-Blogs.com February 13, 2011 at 6:44 am

Did you upload the images using the Featured Image uploader? If not, you need to do that.

Reply

oren February 13, 2011 at 6:57 am

sorry, but can you explain what do you mean by “featured image uploader” ?
what is it exactly ? maybe it’s a stupid question… :)
thanks

Reply

Thesis-Blogs.com February 13, 2011 at 8:16 am

After you add the code that you see on line #2 and #3 above, when you go into the edit post screen, look toward the right at the bottom. You will see a new section where you can upload the featured image. Just upload the image. Have a look at the screenshots I have on this post for more information on that: http://thesis-blogs.com/related-posts-with-thumbnails-without-a-plugin-in-thesis/

Reply

oren February 13, 2011 at 7:12 am

i found this link (http://thesis-blogs.com/wordpress-post-thumbnails-thesis-theme/) and followed its instructions but i only get error message in the ‘after_post” hook where i put the code…
plus i can’t see the the ‘thumbnail menu’ inside the post editor… i can’t figure out what i do wrong..
thanks :)

Reply

Thesis-Blogs.com February 13, 2011 at 8:11 am

Make sure you have inserted the code from Step 1 in your custom-functions.php file. If you can’t see the Featured Image box on the right in the post edit screen, then you don’t have this code inserted. Although this post says to use the Openhook plugin, you can also insert the code from Step 2 in the custom-functions.php file. If you’re using the Openhook plugin, be sure to select the option to execute PHP on the code.

Reply

oren February 13, 2011 at 8:42 am

wow man, thank you so much !!
i do have last question if i may: for some reason i see three thumbs in order and one is floating to the right and is much bigger. any idea on why that happens ?
in ‘before_footer’ hook: see here.
thanks :)

Reply

Thesis-Blogs.com February 13, 2011 at 9:00 am

Make sure you upload all image AFTER you have added the featured image code. On the post with that large image, delete the image and re-upload it. But make sure you have already inserted the code. You also mention a “before_footer” hook. I don’t recommend you add the related posts there. You need to display them after the post. Be sure to follow the exact instructions in the tutorial. Good luck.

Reply

oren February 13, 2011 at 9:16 am

hi.
thank you again, i don’t think this is the reason because in this post for example it displays the image of a different related post larger and the one that was larger in previous post it shows well… so i really don’t know…
just to be sure, i removed the images and uploaded again (uploaded from ‘media’ inside the post, not from computer inside media) but it stayed the same…
so i removed the whole ‘openhook’ code and it works well now…

the thing is i want to add the related posts to display along the footer similar to the footer i have now..
i guess i’ll keep try and see if it works..
thank you very much for your post and your quick help. :)

i want to display the related posts along the footer. do you think it won’t work ?

Reply

Thesis-Blogs.com February 14, 2011 at 8:47 am

I would have to log into your site to see what’s going on. As to putting related posts in the footer, that makes no sense because the footer is rendered across all pages and posts, so what post will the related posts be related to? It will also appear outside the loop if in the footer which is an issue. It’s better to put it after the single post. Just my opinion.

Reply

Tom February 16, 2011 at 9:20 pm

Hi,

I was able to get the related posts with thumbnails in my sidebar to work on my Thesis blog. The only problem is the title link is appearing underneath the thumbnail. How can I get it to appear to the right side of the thumbnail. I’ve tried to play with the CSS, but without success. Please help.

Thank you for the great tutorial!

Tom

Reply

Thesis-Blogs.com February 16, 2011 at 9:31 pm

Not that being unconventional is a bad thing, but why would you put the related posts in the sidebar? Not sure this is the best place for it… I would need to see your site in order to give an intelligent response… thanks for commenting…

Reply

Tom February 16, 2011 at 10:13 pm

Hi,

I posted my website through your contact form. Please check it there. I prefer to have the related posts in my sidebar because I plan to put my Adsense ads after the post strategically. As you can see, I currently have the WP-Thumbie plugin which displays related posts with thumbnails on my sidebar, but it’s not fully compatible with Thesis. I’m planning to remove it as soon as I can get your tutorials to display the same as the plugin version.

If you go to one of the articles, you can see the related posts with thumbnails after the post (created from your tutorial). I just want to make it look like exactly like the plugin version in my sidebar. Also, I plan to put the popular posts with thumbnails below the related posts similar to what you’ve done to your website.

Tom

Reply

anuj@webtricksblog. March 7, 2011 at 12:08 pm

now i can less my one plugin which i am using to show releated post :)

thanks
anuj@webtricksblog. recently posted..How To Add Top Bar Or Header Bar In Thesis Theme Without Any WordPress Plugin

Reply

Thesis-Blogs.com March 7, 2011 at 1:11 pm

You’re welcome.

Reply

Anoop Sudhakaran March 9, 2011 at 4:02 am

Very well explained!! Thanks works perfectly on my blog! :)

Reply

Thesis-Blogs.com March 9, 2011 at 9:47 am

Great! Good to hear.

Reply

Raj Mehta April 5, 2011 at 9:03 pm

I am using Thumbnail For Excerpts plug-in for my site. It fetches first image as thumbnail for my blog. I am not saving my images in featured images. Is there any other method so that i can display first image as thumbnail in related posts or i should start saving pics in feature images.

Reply

Thesis-Blogs.com April 5, 2011 at 9:30 pm

Hi Raj. Yes, this is possible, although I’d have to write a separate tutorial on this one. I’ve done this before where the related post thumbnails are taken from the first image in the post and then resized accordingly using timthumb. It’s a little involved and would have to do a separate post on this, but basically you would implement timthumb and then pull the first image in each post using an applicable script. For now, the quickest way for you to achieve this is to just follow this tutorial and use featured images. If you have a ton of posts, it may not be feasible though.

Reply

Glenn April 17, 2011 at 7:52 pm

I have the same problem as Raj…I have a lot of posts and don’t want to go through each one to add a featured image. Is it possible to have the related posts pull the thumb that Thesis creates when you input the image in the “Post Image & Thumbnail” section under your post?

Reply

Thesis-Blogs.com April 17, 2011 at 8:58 pm

Hi Glenn. I had a closer look at this and you can actually call the Thesis post image or thumbnail using either one of these:

thesis_post_image_info('thumb'); // this calls the built-in Thesis post thumbnail
thesis_post_image_info('image'); // this calls the built-in Thesis post image

You will most likely want the thumbnail, which Thesis resizes automatically based on the main post image URL. Of course, what we’re referring to here is when you upload an image using the media uploader, and then link to it in the “Post Image and Thumbnail/Post Image” section in the edit post screen.

So based on this, go ahead and try this modified code. Note the two lines I have highlighted in yellow.

/ Related Posts By Tag
function related_posts() {
if ( is_single() ) {
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1,
'orderby'=>'rand' // Randomize the posts
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related_posts" class="clear"><h3>Related Posts</h3><ul>';
while( $my_query->have_posts() ) {
$my_query->the_post(); 
$post_image = thesis_post_image_info('thumb');
?>
<li>
	<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>">
		<?php echo $post_image['output']; ?>
	</a>
	<div class="related_content">
		<a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
	</div>
</li>
<? }
echo '</ul></div>';
} }
$post = $orig_post;
wp_reset_query(); ?>
<?php } else { } }

add_action('thesis_hook_after_post','related_posts');

You can take a look at this post on the DIYthemes forum that seems to touch on this and you can search on the specific code above to perhaps find more.

Hope that helps.

Reply

Glenn April 18, 2011 at 11:43 am

Thanks a lot! It worked :) you and your blog have been a huge help for me!

I have one problem though, I set the thesis options to create 125×125 thumbnails for my “Post Images and Thumbnails” so the related posts section is kind of squeezed together. Is there a way to change the size of the thumbnails in the related posts section?

Reply

Thesis-Blogs.com April 18, 2011 at 12:37 pm

Great! Well, not totally following you here but you can change the default Thesis thumbnail size from the Thesis options. I also notice that in the css, I have a width of 100px on line 22 in the tutorial, so you’d also want to adjust that. I’d have to see the site to give you a definitive answer.

Reply

Glenn April 18, 2011 at 7:07 pm

Thanks again. I changed the width on line 22 to 125px which expanded the area the title text of the post went. But, what I was trying to do was shrink the thumbnails so they would be 100×100 instead of 125×125. But, overall I fixed it up so that 4 related post would fit which is fine with me :)

You can check it out here http://www.dropbeatsnotbombs.com on my single posts

You’re blog is great, haha on to the next tutorial :)

Reply

Andrew August 19, 2011 at 10:11 am

Thanks for your help.. now its working fine..
Can we show posts by categories..?
Please give me details then i will adjust with your reply..
thanks
waiting

Reply

Thesis-Blogs.com August 19, 2011 at 10:57 am

Yes, it can be done. The code needs to be adjusted for it. It will have to be in a different post I might do. Thanks for commenting.

Reply

Daniel Lemes April 8, 2012 at 3:43 pm

Helped a lot because i (and a lot of people, i suppose) don’t use featured images. Thanks!

Reply

Melanie Thomassian April 16, 2012 at 1:20 pm

Hi,

First of all, thanks for helping out the Thesis community. It’s much appreciated by those of us who aren’t code savvy.

In this case, however, I’m having difficulty. I tried the above code you gave Glenn, because like him, I don’t want to have to add a featured image for each post (I have over 500). So I tried the above code, and while it’s giving me the related post titles, I’m not getting a thumbnail. Is there something I have to run to generate the thumbnail, or what?
Melanie Thomassian recently posted..Beat Scary Food Bills! Try These Money Saving Tips

Reply

Thesis-Blogs.com April 16, 2012 at 1:50 pm

Hi Melanie,

For that code to work, you must be using the built-in Thesis post thumbnail feature. I’m not sure if you are or not, but the code above does work, so perhaps you’re missing something.

If you are just adding the images to a post using the standard image upload in WordPress, then you may want to use a different method. I’d suggest grabbing the first image in the post and then using Timthumb to resize.

There’s a related post about that here; thesis-blogs.com/use-timthumb-to-add-thumbnail-images/

But you’d need to modify it for the related posts.

There’s no quick, short answer for this and I’d need to see your site in order to fix it. You can click the “hire me” link in the main menu if you’re interested. Thanks.

Reply

Melanie Thomassian April 16, 2012 at 3:21 pm

I thought the code you put in the comments section did call the first image from the post, that’s why I tried it. I didn’t know it depended on the Thesis post thumbnail feature.

I used your original code (in the post), and it works fine. I had featured images in the theme before I installed Thesis. While I still have some posts without featured images from years ago, I’m happy enough with the result.

However, if it’s possible to pull a thumbnail from the first post for every post automatically, surely that would be desirable in most circumstances?
Melanie Thomassian recently posted..Beat Scary Food Bills! Try These Money Saving Tips

Reply

Thesis-Blogs.com April 16, 2012 at 3:26 pm

I misunderstood then. You did say that you were using the code I’d provided above for “glenn” which is using the Thesis thumbnail.

Since you have images inserted into the content of the posts, then yes, using the first image in the post along with Timthumb would be a good way to go.

Reply

Edille May 4, 2011 at 10:48 pm

Hi,

I really appreciate your post but I was just wondering what is the problem with the code? It is not working with me. No thumbnail showing, only title in horizontal line just the title. For now I will switch back to what I have earlier as this cause trouble to the post.

Thanks anyway.

Reply

Thesis-Blogs.com May 4, 2011 at 11:13 pm

Nothing wrong with the code. Make sure you upload the images for your posts using the featured image uploader. That’s what this code uses to display the images. I am using the same code on this site and it works fine. You are most likely not uploading the images correctly.

Reply

Andrew July 15, 2011 at 10:49 am

Hi
How can i use “thesis_post_image” as related posts.
If you don`t understand i will explain it.
I use thesis teasers as post thumb and inserts images in “thesis_post_image” ..
How can i get thesis images as related posts.?
In other words how can i get related posts from thesis teasers.?
thanks
waiting

Reply

Thesis-Blogs.com July 16, 2011 at 6:17 am

Read the comments above which were posted on April 17th. That should be what you’re looking for. Thanks for commenting.

Reply

shashankk chinchli July 15, 2011 at 5:41 pm

I really liked this ! One plugin less for my blog.Thanks a lot dude;)

Reply

Thesis-Blogs.com July 15, 2011 at 8:06 pm

No problem!

Reply

Sandeep Yadav July 17, 2011 at 5:21 am

Hello i m using your code in my Worpdress theme i paste your code in single.php with in , I am using same tags on posts and featured image also but your code not works there nothing showing. Please tell me in detail how i can use your code in Wordpress theme. I hope you will help me.

Thanks

Reply

Thesis-Blogs.com July 17, 2011 at 8:19 pm

The code above will only work on Thesis themes. Since Thesis doesn’t have a single.php file, it would seem that you’re trying to make it work on another theme. You’d probably have to remove lines 6,7 and 41 and paste the code in your single.php file in the applicable place.

Reply

Rosella August 11, 2011 at 6:37 am

It’s not exactly the same theme, sorry for that, but I still ask and hope to get any advise or guidns…
How to show resent posts in nav menu with thumbnails- just like you have here in your blog?

Reply

yusuf September 6, 2011 at 7:54 am

how to add tag
I just copy pasted the this code, but posts are not showing related posts.
[[i can see 2 comment
Make sure you have numerous posts with the same tags. This code returns related posts by tag.
Thanks! Yes worked after putting same tags.]

Reply

Abhinav September 19, 2011 at 12:24 am

Thanks for the article, it worked for me
Abhinav recently posted..Sep 2011 Latest SAIL Bokaro Plant Operator cum Technician Trainee vacancies

Reply

anand October 15, 2011 at 9:07 pm

hey how to make related post using category
anand recently posted..The iPhone 4S: Apple’s Hit Parade Marches On

Reply

Kulwant Nagi October 21, 2011 at 9:18 am

what a wonderful and easy to implement tutorial..

Reply

John Martin December 25, 2011 at 6:15 am

In my blog, i use custom tax.
How to show related post by custom tax.

Here my code on function.php
// Custom Taxonomy Codeadd_action( ‘init’, ‘build_taxonomies’, 0 );function build_taxonomies() { register_taxonomy( ‘type’, ‘post’, array( ‘hierarchical’ => true, ‘label’ => ‘Coupons Type’, ‘query_var’ => true, ‘rewrite’ => true ) );}

Waiting for your replay
John Martin recently posted..Toys R Us

Reply

Leave a Comment

When commenting, you can use basic HTML tags. If you are pasting in any code, please escape it here and then include that code within <pre></pre> tags. Thanks!

CommentLuv badge

Previous post:

Next post: