Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for something like they are using here:

http://www.jennyreviews.com/as-seen-on-tv/triple-joint-formula/

Look under "Related Product Reviews"

I checked the source but could not find the plugin code listed. Maybe its a widget?

share|improve this question

7 Answers 7

up vote 1 down vote accepted

@Scott B,

It looks like they are using a related posts query based on category. When a single Item (post) is being displayed the query looks up the category id then displays posts from the same category.

Add The following code to your sidebar or even to the bottom of single.php depending on where you want to display the "Related Posts"

<!--Begin Related Posts-->
    <?php
        if ( is_single() ) :
        global $post;
        $categories = get_the_category();
        foreach ($categories as $category) :
        $posts = get_posts('numberposts=4&exclude=' . $GLOBALS['current_id'] . '&category='. $category->term_id);
        //To change the number of posts, edit the 'numberposts' parameter above
        if(count($posts) > 1) {
    ?>

    <div class="widget" id="more-category">
    <h3 class="widgettitle"><?php _e('More in',''); ?> &#8216;<?php echo $category->name; ?>&#8217;</h3>
    <ul>
    <?php foreach($posts as $post) : ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>

    <?php } ?>

<?php endforeach; ?>
<?php endif; ?>

<!--/related posts-->

What this is doing is first getting the category of the current post being displayed, omitting the current item from the query, then checking to make sure there are more than 1 posts in that category.

If there are then it will output "More in Your category name" followed by the permalink to the post. If you wanted to show the featured image you could change the last section to look like this:

<div class="widget" id="more-category">
    <h3 class="widgettitle"><?php _e('More in',''); ?> &#8216;<?php echo $category->name; ?>&#8217;</h3>
    <ul>
    <?php foreach($posts as $post) : ?>
    <li><?php the_post_thumbnail(); ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    </div>
share|improve this answer
    
Wow Chris. Great answer. Above and beyond really. The only thing that I'm a bit hesitant about is that in the example it looks to me like the related posts are contextual and not based on category (if you highlight over the items and look at the URLs, you can see they belong to different cats). Anyway, A great answer! –  Scott B Aug 31 '10 at 20:43
    
@Chris_O - Good one! –  MikeSchinkel Aug 31 '10 at 21:58
    
due to some reason it displays teh related posts area twice :{ –  Ayaz Malik Apr 19 '12 at 14:40

I am here to point out a mistake in the code that is shown by @Scott B.

Global $post is actually an array that WordPress uses to display data on the blog. but in this code the array value in $post variable goes to last post in the current category. so after successful display of list of posts. when you see the comments it will always be for the last post mentioned in the list. so if you visit first post from the current category, the comments box will be of last post in the newly generated list of posts. After thinking out a little i fount a solution to that and added another variable to store global $post array. then in the end i assigned the values back to global $post. in this way global post got it current values back and so the correct comment box. Here is the corrected code if anyone needed.

<?php
////////////////////////////////////////////////////////////////
// THE CODE FOR RELATED ARTICLES FROM CURRENT CATAGORY STARTS //
////////////////////////////////////////////////////////////////

if ( is_single() );
global $post;
$current_post = $post;

$category = get_the_category($post->ID);

    $posts = get_posts  (   'numberposts=5&exclude=' 
                    . $GLOBALS['current_id'] 
                    . '&category='
                    . $category->term_id
                    . '&post='
                    . $post->term_id
                );

//To change the number of posts, edit the 'numberposts' parameter above

if(count($posts) > 1) 
{
?>
<div class="widget more-category">
    <h2>
        Related Articles
    </h2>
    <ul>
<?php       
        foreach($posts as $post)
        { 
?>
            <li>
                <a href="<?php the_permalink(); ?>">    
                    <?php the_title(); ?>   
                </a>
            </li>
<?php 
        } 
?>
    </ul>
</div>
<?php 
} 

$post = $current_post;
//////////////////////////////////////////////////////////////
// THE CODE FOR RELATED ARTICLES FROM CURRENT CATEGORY ENDS //
//////////////////////////////////////////////////////////////
?>
share|improve this answer

the follow small function list related post via tags

// related post with wordpress-tags
// wordpress > 2.3
function fbbl_related_posts($limit = 5) {
    global $wpdb, $post, $table_prefix;

    if ($post->ID) {
        $retval = '<ul>';
        // Get tags
        $tags = wp_get_post_tags($post->ID);
        $tagsarray = array();
        foreach ($tags as $tag) {
            $tagsarray[] = $tag->term_id;
        }
        $tagslist = implode(',', $tagsarray);

        // Do the query
        $q = "SELECT p.*, count(tr.object_id) as count
            FROM $wpdb->term_taxonomy AS tt, $wpdb->term_relationships AS tr,     $wpdb->posts AS p WHERE tt.taxonomy ='post_tag' AND tt.term_taxonomy_id = tr.term_taxonomy_id AND tr.object_id  = p.ID AND tt.term_id IN ($tagslist) AND p.ID != $post->ID
                AND p.post_status = 'publish'
                AND p.post_date_gmt < NOW()
            GROUP BY tr.object_id
            ORDER BY count DESC, p.post_date_gmt DESC
            LIMIT $limit;";

        $related = $wpdb->get_results($q);
        if ( $related ) {
            foreach($related as $r) {
                $retval .= '<li><a rel="nofollow" title="' . wptexturize($r->post_title) . '" href="' . get_permalink($r->ID) . '">' . wptexturize($r->post_title) . '</a></li>';
        }
    } else {
        $retval .= '<li>' . __('no related posts', TEXTDOMAIN) . '</li>';
        } $retval .= '</ul>';

        echo $retval;
    }

    return;
}

alternativ you can use the category:

/**
 * related post with category
 * @param: int $limit limit of posts
 * @param: bool $catName echo category name
 * @param: string $title string before all entries
 * Example: echo fb_cat_related_posts();
 */
if ( !function_exists('fb_get_cat_related_posts') ) {
    function fb_get_cat_related_posts( $limit = 5, $catName = FALSE, $title = '<h3>&Auml;hnliche Beitr&auml;ge</h3>' ) {
        global $post;

        $limit = (int) $limit;
        $output  = '';
        $output .= $title;

        $category = get_the_category();
        $category = (int) $category[0]->cat_ID;

        if ( $catName )
            $output .= __( 'Category: ', FB_BASIS_TEXTDOMAIN ) . get_cat_name($category) . ' ';

        $output .= '<ul>';

        $args = array(
            'numberposts' => $limit + 1,
            'category' => $category,
        ); 

        $recentposts = get_posts( $args );
        foreach($recentposts as $catpost) {
            if ($catpost->ID !== $post->ID) {
                setup_postdata($catpost);
                $output .= '<li><a href="' . get_permalink($catpost->ID) . '">' . get_the_title($catpost->ID) . '</a></li>';
            }
        }

        $output .= '</ul>';

        return $output;
    }
}
share|improve this answer

"Yet Another Related Posts Plugin" should do what you want. Just configure the related post template to display the post thumbnail and title.

share|improve this answer

I'm using a plugin called Similar Posts to do exactly what you are after. Like this http://www.australianmusichistory.com/this-month-in-australian-music-september/

It has some nice formatting features that allow you to display thumbnails etc.

share|improve this answer

As the author of Yet Another Related Posts Plugin, I of course will recommend that plugin. :) (Thanks to Steve and Doug for pointing this out as well.) The feedback I get time and time again is that the "related" results received from YARPP tend to better reflect an intuitive notion of "relatedness", largely due to YARPP's algorithm looking at not just shared categories but also shared tags, shared keywords in the title, and shared keywords in the content.

A tutorial on the custom templating functionality mentioned is available here. If you have any questions getting YARPP integrated in your setup, I (and many other YARPP users) can help on the wordpress.org forums' section for YARPP.

share|improve this answer

Simple Tags plugin offers the 'Related posts' functionality based on tags of the post. There is a way to configure the template of how related posts are shown. Not sure if you can take it as far as display the post image, but it's sure worth a try.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.