Best Way to Display Recent Comments with Gravatar without Plugin

Yeah, 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:

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 (+)

  1. dwi says:

    jadi juga za…….tutorialnya…………ak sambil jagain si kecil niy bro

  2. ejak says:

    it works on my blog, thanks ya :)

  3. Shraddha says:

    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.

  4. EntrustHost says:

    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.

  5. Azah says:

    how to exclude author comments?

  6. Printer says:

    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.

  7. thanks for the help you give

  8. Daniel Lemes says:

    Perfect, thanks for help!

  9. DG says:

    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~ :D

  10. mattrock says:

    Great post and just what I was looking for. Thanks!

  11. HAKAN_DY says:

    the title shows “undefined property stdClass post_title”

  12. HAKAN_DY says:

    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>

  13. Amitabha Roy says:

    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

Trackbacks/Pingbacks

  1. 40 Best WordPress Tips and Tutorials of December 2010

Leave a Reply

 

Amazingly Beautiful WordPress Themes