How to add Related Posts with thumbnail without plugin
Description:
Using this snippet you will be able to add to your template a related posts function with thumbnail (if “Use as featured image” are selected) without plugins.
You can insert this code in your single.php file, or in custom post type.
Please do a single.php backup before apply for this code.
The Code:
<?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'=>4, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h2>Related Posts</h2><ul class="relatedPosts">';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><?php the_post_thumbnail(array(40,40)); ?><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
$post = $backup;
wp_reset_query();
?>


