Written by Brian Cray on June 22nd, 2009
Use this code on single.php inside "the loop" to show related posts based on the current post's tags. Customize the HTML and CSS as you see fit. Enjoy!
<h2 id="related">Related Posts</h2>
<?php
$rel_tags = get_the_tags();
foreach($rel_tags as $rel_tag)
{
$rel_tagnames .= $rel_tag->name . ',';
}
$rel_posts = get_posts('tag=' . $rel_tagnames . '&post__not_in' . $post->ID . '&showposts=5');
if (count($rel_posts)) : ?>
<ul>
<?php foreach((array) $rel_posts as $rel_post) { ?>
<li><a href="<?php echo $rel_post->post_name ?>"><?php echo $rel_post->post_title ?></a></li>
<?php } ?>
</ul>
<?php else : ?>
<p>I can't find any related posts, sorry!</p>
<?php endif; ?>