WordPress Recent Comments with Excerpt and Gravatar

WordPress Recent Comments with Excerpt and Gravatar

I build a lot of WordPress websites and it’s great that there are so many plugins available for various functions, but sometimes I run into a situation where the right plugin just doesn’t exist (or if it does I didn’t find it). I needed a widget to display the WordPress recent comments with the comment excerpt and the users gravatar (globally recognized avatar). Here’s what I came up with, modified based on some code from others looking for a similar solution:

Put this into your sidebar template or elsewhere:

<?php $comments = get_comments('status=approve&number=5'); ?>
<h3 class="widget-title">Recent Comments</h3>
<ul class="recomm">
<?php foreach ($comments as $comment) { ?>
    <li class="recomm-wrapper"><?php
        $title = get_the_title($comment->comment_post_ID);
        echo get_avatar( $comment, '53' );
        echo '<span class="recommauth">' . ($comment->comment_author) . '</span>';
        ?> said: "<?php
        echo wp_html_excerpt( $comment->comment_content, 72 ); ?>.."
        on <a href="<?php echo get_permalink($comment->comment_post_ID); ?>"
           rel="external nofollow" title="<?php echo $title; ?>">
           <?php echo $title; ?> </a>
    </li>
<?php }  ?> </ul>

Here’s the CSS styling I’m using (you may need to modify it according to your theme):

ul.recomm {
	margin-left: 0;
	margin-bottom: 36px;
	list-style: none;
}
.recomm-wrapper {
   clear:both;
   min-height: 53px;
   margin-bottom: 14px;
}
.recomm .avatar {
   float:left;
   margin-top:5px;
   margin-right:10px;
   border: #d8d8d8 1px solid;
}
.recommauth {
	font-weight: bold;
}

Contact / Get a Quote