September 7th, 2010 by

From time to time clients ask me implement recent posts. But how about displaying recent comments? And even add gravatar?
Here is a code that displays eight recent comments with avatar image.

    <?php
    $query = "SELECT * from $wpdb->comments WHERE comment_approved= '1'
    ORDER BY comment_date DESC LIMIT 0 ,3";
    $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, 32);
    echo '</a></div>';
    echo $comment->comment_author . " says: ";
    echo comment_excerpt();
    echo '</li>';
    }
    echo '</ul>';
    }
    ?>

2 Responses to “Display recent comments with avatar”

  1. JA says:

    But how to show comments from just one category? I’ve tried several plug ins and can’t find one that works. I’d rather just understand and code it properly. Do you have an idea?

  2. bhobia says:

    This really good info here interesting, thank you very much.

Leave a Reply