Friday, 15 April 2011

Display Most Recent Comments with Gravatars

Have you seen sites that display most recent comments in their sidebar with user gravatars. Well this can be done easily with these codes. Simply paste the following code anywhere you want to display the most recent comments.

    <?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>';
    }
    ?>

To get more or less than 5 comments, change the number 5 on line 2.

0 comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...