ShoutMeLoud – Shouters Who Inspire

Superlinks
≡ Menu
≡ Menu

How to show related posts in wordpress without plugin

bluehost
How to show related posts in wordpress without plugin

I have been talking about lots of wordpress plugin, and at the same time I suggest using minimum wordpress plugin to make your wordpress fast. We have already written a post on how you can add adsense code without any wodpress plugin

Some of the wordpress plugin which I have covered till now are

This is a simple code which will pull up related post. You can put this code either in your single.php file or on the sidebar using Samsarin php widget wordpress plugin

<?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),
        'showposts'=>5, // Number of related posts that will be shown.
        'caller_get_posts'=>1
    );
    $my_query = new wp_query($args);
    if( $my_query->have_posts() ) {
        echo '<h3>Related Posts</h3><ul>';
        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>
        <?php
        }
        echo '</ul>';
    }
}
?>

This code pull of related post using the tag feature.

Try to avoid plugins as much as you can , and use custom wordpress code and hacks to pull up any query.

  • Author Bio

  • Latest Post

Article by Harsh Agrawal

Harsh has written 1097 articles.

If you like This post, you can follow ShoutMeLoud on Twitter. Subscribe to ShoutMeLoud feed via RSS or EMAIL to receive instant updates.


{ 10 comments… add one }

  • rakesh kumar

    This is useless code Harsh, if a user click on the related post fetched with this method in the next post this method again display the same posts as related posts and does not display fresh related posts so i think it is of no use to use this code to generate related post. Do you know any other better idea/code. Waiting for your reply.

    Reply
  • Walter Pinem

    hello.
    thanks for this post.
    I love your ‘related posts’ thumbnail on this post, how to make that?
    What plugin should I use?

    Reply
  • Mohsin Hasan

    I am among those who prefer direct hacking instead of plugin. I am working on new design and using Harsh’s pointer, I was able to do quite a lot of things. I have actually made a function out of this and can use it anywhere now.

    Reply
  • Karthikeyan

    Thanks Harsh! This Code is Really Helpful! Works Good For me! :)

    Reply
  • Analectic

    Hi Harsh

    Simple question, can I use this after the post box instead of sidebars?

    like – add_action(Thesis_hook_after_post, My_related_posts)

    Reply
  • George Serradinho

    Hi Harsh,

    I’m trying this out and the tags is bring nothing back. Does $tags = wp_get_post_tags($post->ID) still work in 2.8?

    I’m using it with the thesis_hook_after_post hook?

    Reply
    • Harsh Agrawal

      George can you post the whole code you are trying to enter into your custom.css file. I will have a look and will let you know.

      Reply
  • mssmotorrd

    It™s the first time I commented here and I must say you share us genuine, and quality information for bloggers! Good job.
    p.s. You have a very good template for your blog. Where did you find it?

    Reply
    • Harsh Agrawal

      I’m glad that you like my posts. Will love to hear more from you.
      Meanwhile I’m using Thesis wordpress theme.

      Reply
  • Harsh Agrawal

    I’m glad that you fixed it. I was about to mail you regarding your problem. Good that you were able to resolve it of your own .
    :)

    Reply

Leave a Comment