Add Related Posts, with Images, to WordPress without a Plugin

by Erik Ford · Blog · Tips & Tutorials · 8 Comments

May 01 2009

We all know that WordPress is highly extensible with the use of plugins. These added files can instantly transform your website in ways you may not have considered from the onset. But, sometimes, you will run into situations where a plugin just cannot perform the exact task you are looking for.

That was the case for me recently, when I was looking for a related post solution that would not simply display a list of links, but a list of images. I also wanted this list to be generated by using tags. There are quite a few plugins that do the trick of generating related post links very well, and some also had an image solution, but none hit the mark of what I needed. That was until I found this helpful tutorial, entitled Show Related Post in WordPress Without a Plugin by Binny V A. He provides a solution for both tags and categories, which is perfect. (In fact, this is a part of a series he is working on that I highly recommend you read and follow.) But, I still needed a way to display thumbnail images instead of post titles.

The Tutorial

And, like a lightning bolt, it hit me. If I store thumbnail images in a custom field for each post, I can simply call that custom field when querying for the related post. Here is my modified code for this process using tags. Open your single.php file and place the code where you would like the related items to appear before the endwhile statement. Please refer to Binny V A’s original post for his original code snippets for both tags and categories.

<?php
	$backup = $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),
		'showposts'=>5, // Number of related posts that will be shown.
		'caller_get_posts'=>1
	);
 
	$my_query = new wp_query($args);
		if( $my_query->have_posts() ) {
		echo '<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_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="149" height="96" alt="<?php the_title_attribute(); ?>" /></a><?php the_title(); ?></li>
 
<?php
	}
		echo '</ul>';
		}
	}
		$post = $backup;
		wp_reset_query();
?>

As you can see, I’ve changed what is displayed in my unordered list item to an image by calling for a predetermined custom field I called “Image”. After that, you can style your unordered list to match your page mock up. Now, how simple was that?

Tutorial Update

With the recent release of WordPress 2.9, we no longer have to store a thumbnail image related to a post in a custom field. We can now leverage the core functionality of <?php the_post_thumbnail(); ?>, which, when properly implemented, will add an option in your Write Post sidebar to choose a post thumbnail from any of the images inside of your post.

First, open your functions.php file and add the following code. Note that I am using the the thumbnail size from the above code snippet and you should replace it with your own dimensions.

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 149, 96, true );

Once you have saved and uploaded your edited file, you will now have a new option in your Write Post sidebar. All that is left to do is replace the following line of code…

<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="149" height="96" alt="<?php the_title_attribute(); ?>" /></a><?php the_title(); ?></li>

with this line of code:

<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a><?php the_title(); ?></li>

You now have an incredibly easy solution of adding a thumbnail, without using custom fields, with the related list of post links. If you want backwards compatibility with posts written for older versions of WordPress, replace that line of code with this conditional statement:

<?php
if ( has_post_thumbnail() ) {
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a><?php the_title(); ?></li>
} else {
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="149" height="96" alt="<?php the_title_attribute(); ?>" /></a><?php the_title(); ?></li>
}
?>

You will now have backward compatibility with themes that were developed for previous versions of WordPress.

Subscribe

Was that good for you? Well, we aim to please. Don't ever want to miss out on feeing this good again? Simply subscribe to our RSS Feed and you will experience this euphoria everytime we post something new. Ok, that is an exaggerartiion, but you will be up to date. You can also have updates sent directly to your inbox with our handy dandy email subscription.

Share

We believe in paying all things forward that comes to us. Please do the same by sharing this post with your family, friends and neighbors. We truly appreciate it.

8 Comments

  1. Dietmar on Sep 14 2009 at 10:25 am

    I always like such tutorials where people can learn to use php and their own skills to solve a problem instead of installing another plugin. Thanx for this one!

  2. kilroy on Jul 23 2009 at 3:21 pm

    very nice, thank you for sharing.

    It would be cool if we could insert images into new post based on keywords or tag. So a image named joe-doe.jpg would be inserted if the content of the post included the text “joe doe”

    I’ve been search and hacking with no success trying to figure this out.. you got anymore lightning bolts?

  3. Jm-Experts on Jul 23 2009 at 11:47 am

    Love this!

  4. Anil on Jun 28 2009 at 3:09 am

    it was really superb tip, i was searching for this long back ..

    check out http://www.shareordie.in

    I want to know how to implement same this code for “Random Posts” like
    example : http://www.picvi.com .

    Help is appreciated …

  5. Shahin - Rolam Shahin Designs on Jun 11 2009 at 7:28 am

    Nevertheless, it’s still a great tutorial Erik :)

  6. Erik Ford on Jun 10 2009 at 1:18 pm

    @Shahin, thanks a lot. When I get the time, I wanted to write a quick little tutorial about custom fields and WordPress.

  7. Shahin - Rolam Shahin Designs on Jun 09 2009 at 2:12 pm

    Nice and neat Erik, just like your theme itself. I always preferred tweaks over plugins. Great tutorial.

    **I think a little bit more details on how the custom field was used in this case could make the post a lot easier for the newbies mate. A lot of people get lost when it comes to custom fields.

  8. dlv on May 25 2009 at 9:22 pm

    great! it’s what i was looking for!
    i also see other plugins (Related post with Thumb), also Binny’s post, but this is perfect… combining both.

    In my case, i change the way the image was taken. I’m using a php thumb generator instead of take the image from custom field. The only reason is that with custom field image & resize the picture look “crispy”, like with hard sharpen on it :) .

    Thanks for this tweak, thanks for share
    adeux!

Leave your Comment

We are gravatar enabled. Personalize your comments by using your own avatar image and let your freak flag fly! It's simple, and better yet, free. Head over to Gravatar and sign up today.

Copyright © 2007 - 2010 pixel8. All Rights Reserved.