Get Recent Comment with Gravatar without plugin

Many WordPress User need to display Recent Comment on the sidebar or footer with Gravatar, most of them using plugin, right now I will show you how to Get Recent Comment with Gravatar without plugin. All need to do just write little bit of code and modified as many you want and you need.

On our recent article, I was cover tips to display most commented post with gravatar without plugin, and I hope you’ll enjoy this tricks too, because this trick also very simple and of course useful.

Let’s get started, open your sidebar.php or footer.php or whatever files you need to display recent comment and add this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?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>';
        echo $url;
        echo get_avatar( $comment->comment_author_email, $img_w);
        echo '</a></div>';
        echo '<div>Par: ';
        echo $url;
        echo $comment->comment_author;
        echo '</a></div>';
        echo '</li>';
    }
    echo '</ul>';
}
?>

This is a trick I got from WP Recipes, you can add some style and make it suitable for your theme.

Alternate Method Display Recent Comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<li class="boxr therecent">
<h3>Recent Comments</h3>
<ul class="recent-comment">
<?php
$comments = get_comments('number=5');
  foreach($comments as $comm) :
  $url '<a href="'. get_permalink($comm->comment_post_ID).'#comment-'.$comm->comment_ID .'" title="'.$comm->comment_author .' | '.get_the_title($comm->comment_post_ID).'">' $comm->comment_author . '</a>';
?>
<li>
<?php echo get_avatar($comm->comment_author_email, 30); ?>
<strong><?php echo $url; ?></strong>
<p><?php echo $comm->comment_content; ?></p>
</li>
<?php
  endforeach;
?>
</ul>
</li>

This code more easy to implemented and use, after you write this code, you need to open style.css ad add css code

1
2
3
4
5
6
7
8
.recent-comment li {
    displayblock;
    border-bottom1px dotted #ccc;
}
.recent-comment .avatar-30 {
    floatleft;
    margin-right10px;
}

And done, I hope you enjoy our Recent Comment with Gravatar without Plugin tips