Display Related Posts in WordPress without a Plugin


Paste the following code in single.php where you want to display related posts


<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'posts_per_page'=>3,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

<?php
endwhile;
}
wp_reset_query();
}
?>
Advertisements

12 thoughts on “Display Related Posts in WordPress without a Plugin

  1. Could you explain this a bit more? Why do I need to do this? Where do I paste this exactly? What will happen when I paste this? Thanks so much!

  2. Hey There. I discovered your weblog using msn. That is an extremely well written article.
    I’ll be sure to bookmark it and return to read more of your helpful information. Thanks for the post. I’ll certainly comeback.

  3. I will be convinced We’ve look at this very same form of statement somewhere else, it must be more popular while using public. [url=http://loveplugins.com/tag/wishlist-member/]techniques[/url]

  4. I all the time used to study piece of writing in news papers but now as I am a user of internet thus from now
    I am using net for articles, thanks to web.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s