Post Pic

Display most recent comments with Gravatar

Is your blog popular? Do you got receive lots of comments from your readers? If yes, what about displaying the most recent comments in your blog sidebar (or elsewhere) to let your visitors knowing about the discussion?

In order to display the 5 most recent comments with their gravatars, paste this code anywhere on your theme files. To get more or less than 5 comments, change the number 5 on line 2.

<?php
$query = "SELECT * from $wpdb->comments WHERE comment_approved= '1'
ORDER BY comment_date DESC LIMIT 0 ,5";        
$comments = $wpdb->get_results($query);

if ($comments) {
    echo '<ul>';
    foreach ($comments as $comment) {
        $url = '<a href="'. get_permalink($comment->comment_post_ID).'#comment-'.$comment->comment_ID .'" title="'.$comment->comment_author .' | '.get_the_title($comment->comment_post_ID).'">';
        echo '<li>';
        echo '<div class="img">';
        echo $url;
        echo get_avatar( $comment->comment_author_email, $img_w);
        echo '</a></div>';
       
        echo '<div class="txt">Par: ';
        echo $url;
        echo $comment->comment_author;
        echo '</a></div>';
        echo '</li>';
    }
    echo '</ul>';
}
?>

3 Responses

Jul 19 2011 19:24

This was just what I was looking for – thanks! Worked great for me after I added global $wpdb; to the beginning.

Jul 21 2012 12:29

Thanks! I’ve been fiddling around with gravatar this past month, and I love it so far :) I love actually finding the exact response I needed for a question.

Mar 18 2013 13:09

thanks you for this post! Been looking for ages :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox