Display Related Posts with Thumbnails in WordPress

Share:
Share

Obviously nobody wants his visitor to leave the Blog after reading one Post or Article. There are a number of ways to let the visitor stay around. One of them is showing Related Posts or Popular Posts at the end of the Article which grabs the visitor attention and makes him go through some more articles of same type. Here I’m showing a simple way to Display Related Posts at the end of the Article.

Sm2 in 10 Useful WordPress Coding Techniques

What to do?

Simply paste this code after the the_content() function in your single.php file:

<?php
$original_post = $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
 echo '<h2>Related Posts</h2>';
 $first_tag = $tags[0]->term_id;
 $args=array(
 'tag__in' => array($first_tag),
 'post__not_in' => array($post->ID),
 'showposts'=>4,
 'caller_get_posts'=>1
 );
 $my_query = new WP_Query($args);
 if( $my_query->have_posts() ) {
 echo "<ul>";
 while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <li><img src="<?php bloginfo('template_directory'); ?>/timthumb/timthumb.php?src=<?php echo get_post_meta($post->ID, "post-img", true); ?>&h=40&w=40&zc=1" alt="" /><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
 <?php endwhile;
 echo "</ul>";
 }
}
$post = $original_post;
wp_reset_query();
?>

What does this do?

Here we are using TimbThumb, a PHP image-resizing script to automatically resize images to 40 by 40 pixels.

Once you paste this code in your theme, the first tag of the post is used to fetch related posts from to the Blog. Here we are showing 4 related posts as example. You can change this number on Line 10.

Check out how you can do it with a Plugin

View the Page here


Share:
Share

Tags: , ,

4 Responses to “Display Related Posts with Thumbnails in WordPress”

  1. saad 23. Nov, 2009 at 12:51 pm #

    I think wp-thumbie from blogsdna is a good solution to show related posts with thumbnails.Wot do u say?
    saad´s last blog ..Read RSS/Atom Feeds, HTML, and Text in Newspaper Format My ComLuv Profile

    [Reply]

    Themepremium Reply:

    I agree with you Saad.. I’m using it on one of my blog and its good! Just the link on all pages is quite annoying
    Themepremium´s last blog ..BuySellads WordPress Plugin : Manage Buysell Ad Slots Easily My ComLuv Profile

    [Reply]

    saad Reply:

    You can simply delete the code from the plugin.I modified it for my need.I think you are talking about Wp-thumbie from BlogsDna link if i am not wrong.
    saad´s last blog ..Change Initial View of PDF and Version My ComLuv Profile

    [Reply]

Trackbacks/Pingbacks

  1. Twitted by enked - 24. Nov, 2009

    [...] This post was Twitted by enked [...]

Leave a Reply

CommentLuv Enabled