Get Recent Comment with Gravatar without plugin

January 24th, 2010 By WP Tricks Posted in Tips

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 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

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

Sponsored Links


This tricks I got from WP Recipes, you can add some style and make it suitable for you themes.

Alternate Method Display Recent Comment

<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

.recent-comment li {
	display: block;
	border-bottom: 1px dotted #ccc;
}
.recent-comment .avatar-30 {
	float: left;
	margin-right: 10px;
}

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

Related Posts

  1. Download Gravatar Widget Plugin
  2. How To Automatically Refuse Comment without Plugin
  3. How To Load Gravatar on the Fly
  4. The Right Way to Change The Default Gravatar in WordPress
  5. Remove nofollow attributes from comment text
  6. How To Display Recently Registered Users on WordPress

Get Recent Comment with Gravatar without plugin On The NET

Display Recent Comment with Gravatar without plugin # WordPress ...

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 ... read more

Source: http://wptricks.net/get-recent-comment-with-gravatar-without-plugin/

Display recent comments with Gravatars – No Plugin | Wordpress ...

... Gravatar – without a plugin. The code used in this post is very similar to the recent comment ... get_avatar( $comment->comment_author_email, $img_w); echo ' read more

Source: http://fuzeo.com/tutorials/display-recent-comments-with-gravatars-no-plugin/

Comment # WordPress Tricks & Tips

... 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. read more

Source: http://wptricks.net/tag/comment/

Get Recent Comments Plugin | Blogging Without A Blog

To show avatars/gravatars, you’ll need to add “%gravatar” (without the ... I’ll have to take a look at this “get recent comment” plugin with the gravatar. read more

Source: http://bloggingwithoutablog.com/feff-its-good-for-the-ego/

Gravatar # WordPress Tricks & Tips

... 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. read more

Source: http://wptricks.net/tag/gravatar/


Amazing Touch


14 Comments

  1. oke thanks for trick i will try it on my blog :D

    • Got some problem? Please let’s me know…

  2. thanks so much, I will applecate it.

  3. thanks so much, I will applecate it.

  4. mike

    Can someone help me I don’t know how to limit comment lenght. I’m using the second code (the one that’s easier)

  5. Without plugin… wow its really great..
    I added this code..
    thanks.

  6. It’s great since we can do this w/o a plugin.
    I’ll on my site. Thanks :-)

  7. nice triks and i’ve been try to my bottom template but how can i change size of the gravatar image cause i’ts so big at my theme. rgds

  8. How about instead of the comments entire content just an excerpt of it? How could I possibly do that? Thanks for this amazing code btw.

  9. Nice snip it, but how do I make it to display comments anywhere and not just on the page that has comments?

    I have this running in my footer, and it works when you land on a page with comments, but once you goto a page with no comments you see nothing accept the avatars

    • Hi, I got the same question as well. How can we make this code more flexible, so that it can be placed on any page and still be useful? I’d love to place these recent comments on a “non-comments” page, if you know what I mean.

  10. I have to try this right away, thank you so much for sharing, that was exactly what I was looking for!

  11. I have been working with the comments on my page for few days now to make it not so boring. Your tips sounds great and I’ll surely try it. Will this work regardless of the browser we are using?

  12. My Q,did we must add a new CSS file to make this work? Sorry i;m new with wp.


4 Trackbacks


Add Your Comment

Gravatar