Related Posts without a Plugin

<?php // related posts based on first tag of current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo ‘<h3>Related Posts</h3>’;
$first_tag = $tags[0]->term_id;
$args = array(
‘tag__in’ => array($first_tag),
‘post__not_in’ => array($post->ID),
‘showposts’ => 7, // how many posts?
‘caller_get_posts’ => 1
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) { ?>
< ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
< li>< a rel=”nofollow” href=”<?php the_permalink(); ?>”><?php the_title(); ?>< /a>< /li>
<?php endwhile; ?>
< /ul>
<?php } ?>
<?php } ?>


Related Posts