related posts without plugin Recently i was working on speeding up my site. I reduced the usage of as many plugins i could to speed up wordpress. At last i thought of finding an alternative to the Yet another Related Posts plugin too. I thought of display the related posts without using any wordpress plugin. So, i googled for it and got the code. But there were some problems i found with the code. But this is a serious error that most of them will go unnoticed.

Since most of the site having the same posts copied from one another, almost all the codes i got were the same.

Problem with Displaying Related Posts without any Plugin

The problem i found is that, after the related posts were displayed at the end of the post, the comments displayed on the current page comes from the last post in the related posts list. I found this fault the next day only. So i thought of finding a fix for the code. I have made some changed to the codes and here are them.

Showing Related Posts without Plugin based an Categories

<?php $categories = get_the_category($post->ID);
  if ($categories) { $category_ids = array();   
foreach($categories as $individual_category)
$category_ids[] = $individual_category->term_id;
    $args=array(
        'category__in' => $category_ids,
        'post__not_in' => array($post->ID),
        'showposts'=>5, // Corresponds to Number of related posts to be shown.
        'caller_get_posts'=>1
    );
 $my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts…..</h3><ol>';
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 '</ol>'; } } $post = $backup; wp_reset_query(); ?>

Now paste the code in single.php ust before the post ends or just after <?php the_content(); ?>.<?php the_content(); ?>

Displaying Related Posts based on Tags without Plugin

<?php
  $backup = $post;
  $tags = wp_get_post_tags($post->ID);
  $tagIDs = array();
  if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i < $tagcount; $i++) {
      $tagIDs[$i] = $tags[$i]->term_id;
    }
    $args=array(
      'tag__in' => $tagIDs,
      'post__not_in' => array($post->ID),
      'showposts'=>5, // Corresponds to Number of related posts to be shown.
      'caller_get_posts'=>1
    );
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Related Posts…..</h3><ol>';
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 '</ol>'; } } $post = $backup; wp_reset_query(); ?>

Paste the code just after the post content to display the related posts. You can change the value of ‘showposts’=>5 to a desired number. It corresponds to the number of related posts to be displayed.

Here is how the Related Posts are Displayed Based on Categories (I use this style with a little css applied)

My Related posts without plugin

If you have got some css code for the related posts code then just apply to this code easily. Just put this the whole code inside the div tag corresponding to the related posts css.

If you have any trouble displaying the related posts in your site, feel free to comment down. I ll try my best to help you…


Amal Roy is the Founder-Editor of Computing Unleashed. A Technology Enthusiast, Windows Adept and a Proud Geek! | Connect with him on Google+