When working with wordpress themes sometimes its handy to have little code snippets for dropping into your theme, this highly customisable snippet will grab your most recent comments and loop through them as many times as needed,

Features:
Comment Count
Comment Status
Comment Type
Comment Gravatar
Comment Link
Post Title with Permalink
Check out the source below…
Source Code
<?php
$recent_comments = get_comments( array(
'number' => 5,
'status' => 'approve',
'type' => 'comment'
) );
foreach ($recent_comments as $comment)
{
?>
<li>
<a href="<?php echo get_permalink($comment->comment_post_ID);?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>">
<?php echo get_avatar( $comment->comment_author_email, '55' ); ?>
</a>
<h3>
<a href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php echo $comment->comment_ID;?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>">
<?php echo get_the_title($comment->comment_post_ID); ?>
</a>
</h3>
By: <?php echo $comment->comment_author;?>
</li>
<?php
}
?>
OK so lets walk through the code..
get_comments()
Using the WordPress function get_comments() we can use an array of arguments to pass into this function to retrieve what we need, the wordpress codex page gives a small but tidy explanation of this function.
<?php $defaults = array(
'author_email' => '' ,
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_id' => '',
'status' => '',
'type' => '',
'user_id' => '' ); ?>
Passing in our arguments of number=5, status=approved & type=comment (this should be self explaining) we want to show only 5 comments in our list, they must be approved, and have the type comment,
TIP
If we didnt use the type=> ‘comment’ argument then the results, would include any ‘pingbacks‘ which is not what we want to show, only comments…
Next we use the PHP foreach loop so we iterate through each result, and display our content anyway we want..
if you were to use the PHP print_r() function before the foreach loop, the output will show you all values in that array,
Example
<?php
echo "<pre>";
print_r($recent_comments);
echo "</pre>";
?>
the print_r() function above will print out something similar to this
[0] => stdClass Object
(
[comment_ID] => 23387
[comment_post_ID] => 32
[comment_author] => Marty
[comment_author_email] => myemail@myemail.com
[comment_author_url] => http://www.martin-gardner.co.uk
[comment_author_IP] => 11.111.11.111
[comment_date] => 2010-09-22 08:09:24
[comment_date_gmt] => 2010-09-22 07:09:24
[comment_content] => the content of the comment
[comment_karma] => 0
[comment_approved] => 1
[comment_agent] => Mozilla
[comment_type] =>
[comment_parent] => 0
[user_id] => 2
[comment_subscribe] => N
)
This is just one result from my array,
Generate a Gravatar
Using a call to the comment_author_email value we can do a check to see if that user has a gravatar icon assosiated with there account… if they have then we can print it to the screen, if not then we can just show our own default gravatar picture…
<?php echo get_avatar( $comment->comment_author_email, '55' ); ?>
Generate The Permalink
Using the wordpress function get_permalink() we can in our value for the post ID which will produce the permalink needed for the post
<?php echo get_permalink($comment->comment_post_ID);?>
End Results
After you have all your code in the right places you should end up with something like this

Final Thoughts
Using the get_comments() function we can cut down on the ammount code we have to write, I know theres lots of other variations out there that will do nearly everything you need, but this dosnt need any messy select statements or crazy database joins to get the job done..
What do you rekon? have a better, simple way? of getting the job done, feel free to share it..
Senior Web Developer who combines passionate coding skills, strategic vision with strong hands on leadership and enormous personal drive. I create re-usable industry standard code that is used time and time again, producing outstanding results for every project I work on!
I also like Classic motorbikes, Quads & electronics using the Arduino.
How To Add Recent Comments To Your WordPress Theme The Easy Way
When working with wordpress themes sometimes its handy to have little code snippets for dropping into your theme, this highly customisable snippet will grab your most recent comments and loop through them as many times as needed,

Features:
Check out the source below…
Source Code
<?php $recent_comments = get_comments( array( 'number' => 5, 'status' => 'approve', 'type' => 'comment' ) ); foreach ($recent_comments as $comment) { ?> <li> <a href="<?php echo get_permalink($comment->comment_post_ID);?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>"> <?php echo get_avatar( $comment->comment_author_email, '55' ); ?> </a> <h3> <a href="<?php echo get_permalink($comment->comment_post_ID);?>#comment-<?php echo $comment->comment_ID;?>" title="<?php echo $comment->comment_author;?> on <?php echo get_the_title($comment->comment_post_ID); ?>"> <?php echo get_the_title($comment->comment_post_ID); ?> </a> </h3> By: <?php echo $comment->comment_author;?> </li> <?php } ?>OK so lets walk through the code..
get_comments()
<?php $defaults = array( 'author_email' => '' , 'ID' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_id' => '', 'status' => '', 'type' => '', 'user_id' => '' ); ?>Passing in our arguments of number=5, status=approved & type=comment (this should be self explaining) we want to show only 5 comments in our list, they must be approved, and have the type comment,
Next we use the PHP foreach loop so we iterate through each result, and display our content anyway we want..
if you were to use the PHP print_r() function before the foreach loop, the output will show you all values in that array,
Example
the print_r() function above will print out something similar to this
[0] => stdClass Object ( [comment_ID] => 23387 [comment_post_ID] => 32 [comment_author] => Marty [comment_author_email] => myemail@myemail.com [comment_author_url] => http://www.martin-gardner.co.uk [comment_author_IP] => 11.111.11.111 [comment_date] => 2010-09-22 08:09:24 [comment_date_gmt] => 2010-09-22 07:09:24 [comment_content] => the content of the comment [comment_karma] => 0 [comment_approved] => 1 [comment_agent] => Mozilla [comment_type] => [comment_parent] => 0 [user_id] => 2 [comment_subscribe] => N )This is just one result from my array,
Generate a Gravatar
Using a call to the comment_author_email value we can do a check to see if that user has a gravatar icon assosiated with there account… if they have then we can print it to the screen, if not then we can just show our own default gravatar picture…
Generate The Permalink
Using the wordpress function get_permalink() we can in our value for the post ID which will produce the permalink needed for the post
End Results
After you have all your code in the right places you should end up with something like this

Final Thoughts
Using the get_comments() function we can cut down on the ammount code we have to write, I know theres lots of other variations out there that will do nearly everything you need, but this dosnt need any messy select statements or crazy database joins to get the job done..
What do you rekon? have a better, simple way? of getting the job done, feel free to share it..
Marty
Latest posts by Marty (see all)