Dukeo » WordPress » WordPress Related Posts: How To Show Them Without Any Plugin

WordPress Related Posts: How To Show Them Without Any Plugin

Steven One response WordPress

A very interesting way to improve your blog for both readers and search engines is to work on cross-linking your posts with other related posts.

First of all, it will give the opportunity to your visitors to continue reading content they might be interested in.

Secondly, it’s a great way to improve your WordPress blog SEO-wise: search engines love cross-links to relevant content.

An easy way to display related posts is to use post tags. To display this list of related posts, you must paste this code within the loop:


$number = '5';
$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo '<h3>Related Posts</h3>';
  $first_tag = $tags[0]->term_id;
    'tag__in' => array($first_tag),
    'post__not_in' => array($post->ID),
  $my_query = new WP_Query($args);
  if( $my_query->have_posts() ) { ?>
      <?php 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>
      endwhile; ?>
  <?php }

This snippet of code allows you to easily display a list of 5 posts related to current post based on its tags, without installing any plugin that would use more resources on your webhost.

If you want to display more than 5 posts, edit the number in the first line of this code.

If you enjoyed this article,
Get email updates

Join for Free »

One Comment

  1. Paul Salmon

    Thanks for the code. I’m sure it will be beneficial for many people that want to show related posts, which will help with deeplinking to older posts in your blog.