Best Way to Display Recent Comments with Gravatar without Plugin
20th Dec 2010 | Posted by reza | 21 CommentsYeah, I know, there are tons article to display recent comments with gravatar, but I found that most of them are using SQL query to get the comments.
The code I use here is simpler, and faster because it don’t need to make a DB query.
You may also be interested in the following WordPress tutorial related articles:
- WordPress Comment Design: Tutorials, Considerations, Plugins and Design Inspiration
- Create Your Own Image to Change The Default Gravatar in WordPress
- Easy Way to Highlight Author Comments with a Different Background Color
- Tutorial : Separating Trackbacks From Comments in WordPress 2.7, 2.8 & 2.9
Here is the code:
<?php $comments = get_comments('status=approve&number=5'); ?> <ul> <?php foreach ($comments as $comment) { ?> <li> <?php echo get_avatar( $comment, '35' ); ?> <a href="<?php echo get_permalink($comment->ID); ?>#comment-<?php echo $comment->comment_ID; ?>" title="on <?php echo $comment->post_title; ?>"> <?php echo strip_tags($comment->comment_author); ?>: <?php echo wp_html_excerpt( $comment->comment_content, 35 ); ?>... </a> </li> <?php } ?> </ul>
Ok, here’s some code explanations:
- $comments = get_comments(‘status=approve&number=5′) -> get 5 latest approved comments
- get_avatar( $comment, ’35′ ) -> get and display avatar with 35x35px size
- get_permalink($comment->ID) and $comment->comment_ID -> get the link to the comment
- strip_tags($comment->comment_author) -> display comment author
- wp_html_excerpt( $comment->comment_content, 35 ) -> display the comments up to 35 characters
You can place this code to your theme sidebar or wherever and then add css style to meet your design needs
20 Comments to “Best Way to Display Recent Comments with Gravatar without Plugin”
Add Comments (+)-
-
it works on my blog, thanks ya
-
My requirement is to show recent comment on each post. For example i have two categories named Ramayana and Mahabharat. In Ramayana i have posts like Rama, Seeta, Laxman and In Mahabharat Pandav & Kaurav.
Lets say someone cpmmented kaurav. Instead of showing the recent post only in Kaurav my codes(mentioned below) shows the recent comment everywhere i mean on Rama, Seeta, Laxman, Pandav and Kaurav.
Is there any way to solve this.
-
I tried this on my site the recent comments with the users came out fine but when I clicked the comments to take me to the posts it took me instead to the most recent post.
-
how to exclude author comments?
-
Great! thank you.
This works for me only when I change the: “get_permalink($comment->ID)” to “get_permalink($comment->comment_post_ID)” Otherwise the permalink won’t work.-
Worked like a charm. Thanks!
-
Thanks. It worked for me too. Otherwise the permalink becomes same for all comments
-
-
thanks for the help you give
-
Perfect, thanks for help!
-
Just two simple questions:
1. What a minimum version of WordPress to use this code?
2. Trackback and pingback will also show with this code? If that so, how to remove them (trackback and pingback) in this code…?Thanks in advance~
-
Great post and just what I was looking for. Thanks!
-
the title shows “undefined property stdClass post_title”
-
got it.. for WP 3.5 change <code><?php echo $comment->post_title; ?></code>to<code><?php echo get_the_title($comment->comment_post_ID); ?></code>
-
Hi,
I am getting the same permalink for all comments using this code. Only comment id in the link is correctly generated. Any help or pointers would be appreciated.
Thanks
jadi juga za…….tutorialnya…………ak sambil jagain si kecil niy bro