Show Related Posts in Wordpress without any plugins
It is always good to avoid as many plugins as you can and instead implement the feature with a code, since too many plugins hamper the loading time of the website and thus leading to a very slow website.
How to implement Related Posts in Wordpress without any plugins?
It is very simple. Just go to your single.php file in Wordpress dashboard. (Dashboard -> Appearance -> Editor -> Single.php) and put this code where you want to display the posts.
<?php
$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="Permanent Link to <?php the_title_attribute(); ?>">
<?php the_title(); ?></a></li>
<?php
}
echo '</ul>';
}
}
?>
That is all. Now it will automatically generate all the related posts based on the tags used in the particular post. Do you also have any such methods? Then do let me know of it via comments.
Leave a Reply
2765 views, 2 so far
today |


May 29th, 2009 at 2:17 pm
hey, its nice, its better, not to use plugins when we can do the task manually by adding some codes… Going to use it on my blog.
May 30th, 2009 at 5:52 pm
Hey thanks it’s nice one but how about showing related post based on category?
May 30th, 2009 at 11:22 pm
Thanks Rick for the comment
Will check it and let you know
June 11th, 2009 at 11:40 pm
Hey this is very handy and useful Vikas. Specially for people like me who relies more on codes rather then plugin.
August 20th, 2009 at 5:50 pm
hey vikas,
i have tried this and it doesn’t seem to work when you post the related above the comments box… it shows the related properly but it does not show the comments posted … any ideas?
January 3rd, 2010 at 5:22 pm
how about adding thumbnails to it