Show related posts in WordPress without a plugin

There are plugins that provide you with the ability to show related posts below a post. But it’s possible to do this without using plugins.

This code uses tags attached to posts to show 5 related posts in a list.

<?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), // do not display the current post
'showposts'=> 5, // Number of related posts that will be shown.
'no_found_rows' => true
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<h3>Recommended posts to read</h3><ul>';
while ($my_query->have_posts()) :
$my_query->the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Open <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; echo '</ul>'; }}
wp_reset_postdata();
?>

You should place it below the content in single.php.