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



Pingback: designfloat.com
oke thanks for trick i will try it on my blog
Got some problem? Please let’s me know…
Pingback: Cara Bikin Recent Comment Tanpa Plugin / ID-JAUHARI
Pingback: Free WordPress Theme: Gunungkidul / WordPress Themes, Plugins & Development
thanks so much, I will applecate it.
thanks so much, I will applecate it.
Can someone help me I don’t know how to limit comment lenght. I’m using the second code (the one that’s easier)
Pingback: Sélection de fonctionnalités WordPress sans plugins | Blog de Johan BLEUZEN
Without plugin… wow its really great..
I added this code..
thanks.
It’s great since we can do this w/o a plugin.
I’ll on my site. Thanks
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
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.
Pingback: WordPress: Kommentare ohne Plugin mit Avatar anzeigen » Plugin, Kommentaren, Codeschnipsel, Suche, Dementsprechend, Avatare » QuerBlog.de
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.
I have to try this right away, thank you so much for sharing, that was exactly what I was looking for!
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?
My Q,did we must add a new CSS file to make this work? Sorry i;m new with wp.
you can copy paste those css styles into your existing css file if you have one, this will work without the css but would not look as pretty.
thanks a lot! much appreciated..
I like the second one with css.
How can we use this to show only an excerp of the comment?
The second one in CSS is alot better. Thank you for sharing.
Hello,
This is great thank you.
It’s tips and hints like this that have fully endeared me to the WP community!
Thanks,
Matt
It’s great to do stuff without plug-ins. It is very easy to add a lot of unnecessary bloat to your website by installing a lot of plugins.
Thanks for the tips. I’d like to use the second one on my site, but I need to show the relative time of the comment too (eg, 5 minutes ago, etc) Is that possible? I’ve actually been trying to do it all day, but nothing works
A great tutorial which I will implement. Very easy to understand. I will applecate it.
Thanks for great post, for sure i will use it in my work
Thanks again
Thanks so much, i’ll share to my community.
Is there any specific reason why plugin shouldn’t be used? Is there any specific advantage why the code posted above should be used instead?
neat code man
10x gona use it
so how do you get the comment excerpt instead of full comment with the 2nd code example?
Without plugin… wow its really great..
I added this code..
thanks.
Very nice scipt, yesterday I trying this script, very useful. Thank’s
i want to display same your site, where do i put code into sidebar
Put it on
sidebar.phpand your are done..into end sidebar.php, display same your site not, display content
thanks, i done it, but how to want to abstract comment?
Pingback: How To Automatically Refuse Comment without Plugin | WordPress News Wire
this works g8, i was fed up with many plugins and got frustrated..this is awesome…some help i needed in this if u can…Can u tell me how to get the comment limited characters…it is displaying full comment…i want it to be limited characters..plz tell me where to change the code….
For those of you that are having problems defining a custom excerpt length, I’ve got a solution for the second method!
Add this to the first php block:
$length = 40; // adjust to needed length$thiscomment = $comm->comment_content;
if ( strlen($thiscomment) > $length ) {
$thiscomment = substr($thiscomment,0,$length);
$thiscomment = $thiscomment .' ...';
}
Then instead of using:
echo $comm->comment_content;Use:
echo $thiscomment;Hope this helps everyone!
Thanks you…
Pingback: Reading up on “comments” again « Feet up, eyes closed, head back