DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

When a reader finished reading one of your blog posts, why not suggesting him other article he might like as well? Here’s a quick tip to automatically display related posts (based on category) below the current post.

First, paste the code below into the functions.php file from your theme.

// "More from This Category" list by Barış Ünver @ Wptuts+
function wptuts_more_from_cat( $title = "More From This Category:" ) {
    global $post;
    // We should get the first category of the post
    $categories = get_the_category( $post->ID );
    $first_cat = $categories[0]->cat_ID;
    // Let's start the $output by displaying the title and opening the <ul>
    $output = '<div id="more-from-cat"><h3>' . $title . '</h3>';
    // The arguments of the post list!
    $args = array(
        // It should be in the first category of our post:
        'category__in' => array( $first_cat ),
        // Our post should NOT be in the list:
        'post__not_in' => array( $post->ID ),
        // ...And it should fetch 5 posts - you can change this number if you like:
        'posts_per_page' => 5
    );
    // The get_posts() function
    $posts = get_posts( $args );
    if( $posts ) {
        $output .= '<ul>';
        // Let's start the loop!
        foreach( $posts as $post ) {
            setup_postdata( $post );
            $post_title = get_the_title();
            $permalink = get_permalink();
            $output .= '<li><a href="' . $permalink . '" title="' . esc_attr( $post_title ) . '">' . $post_title . '</a></li>';
        }
        $output .= '</ul>';
    } else {
        // If there are no posts, we should return something, too!
        $output .= '<p>Sorry, this category has just one post and you just read it!</p>';
    }
    // Let's close the <div> and return the $output:
    $output .= '</div>';
    return $output;
}

Once done, open your single.php file and call the function as shown below, where you’d like to display the related posts:

<?php echo wptuts_more_from_cat( 'More From This Category:' ); ?>

Thanks to WP Tuts for the cool tip!

Tags

5 Comments

  1. Glad you liked it! 🙂

  2. I made a little code snippet for showing related post based on every term used with the article. Try it out! http://wp.me/p38LaD-8u

  3. This is a nice snippet to have, but for my purposes I wanted it to display a random list so I added:

    ‘orderby’ => ‘rand’

    into the $args and it works great.

  4. Nice 😉 thanks for it;) it tried it and it worked 😉

  5. In using this on one of my blogs I was having an issue with the output of the related posts, and the comments that appeared below.

    The issue I was having was that the comments that were appearing below the post were from a post linked to in the related posts output. I fixed this by adding a “wp_reset_query();” in the function immediately before the else part.

    This function, with the modifications to show random related posts and with the reset query can be seen at http://blogmovers.net/related.txt

Leave a Comment

Your email address will not be published. Required fields are marked *