How to show related posts in wordpress without plugin

by Harsh Agrawal on April 18, 2009

in Wordpress

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 wordpress_logo

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

Add related post using Yet another related post plugin

Wordpress ALL in one SEO plugin : A complete review

Platinum SEO pack plugin : Alternative to ALL on one SEO plugin

Feed subscription reminder plugin

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.

Related posts:

  1. Show Related Post In Wordpress With Thumbnails : Wordpress Plugin
  2. Wordpress Related Posts With Horizontal Thumbnails For Thesis Theme
  3. Add related post in wordpress with yet another related post plugin
  4. Show Simple and Decent Featured Posts Slideshow Wordpress Smooth Slider
  5. Wordpress Landing sites plugin to decrease wordpress blogs bounce rate
  6. Twitter Live blog wordpress plugin : Show your Tweets on your blog
  7. Subscribe To Comment Wordpress plugin : Essential WP Plugin
  8. Wordpress plugin: How many plugins we should use?


{ 1 trackback }

Complex Optimization Guide Part 11. Wordpress Optimization | odd creations blog
September 24, 2009 at 14:40

{ 8 comments… read them below or add one }

1 Problogger Making Money Online With Blogging April 19, 2009 at 00:53

Hi Harsh,

I added the code to my blog’s Single.php file. But they do not appear just below the current post. How can I manage that? Thanks for the code!

Reply

2 Problogger Making Money Online With Blogging April 19, 2009 at 01:31

I managed to show them below current post. The place where you place the mentioned code in Single.php is very important.

Reply

3 Harsh Agrawal April 19, 2009 at 13:20

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

4 mssmotorrd May 3, 2009 at 18:25

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

5 Harsh Agrawal May 4, 2009 at 10:49

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

Reply

6 George Serradinho June 20, 2009 at 12:03

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?
.-= George Serradinho ´s last blog ..Happy Birthday my Boy “ You are 7 Today =-.

Reply

7 Harsh Agrawal June 20, 2009 at 12:08

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

8 jack January 18, 2010 at 13:20

This doesn’t work.

Reply

Leave a Comment

Previous post:

Next post: