Recent Tweets

How To Add Recent Comments To Your WordPress Theme The Easy Way

How To Add Recent Comments To Your WordPress Theme The Easy Way

Last Updated: September 29, 2010 1:54 pm 0 comments

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,


Comment Count
Comment Status
Comment Type
Comment Gravatar
Comment Link
Post Title with Permalink

Check out the source below…

Source Code

$recent_comments = get_comments( array(
  'number'    => 5,
  'status'    => 'approve',
  'type'	=> 'comment'
) );

foreach ($recent_comments as $comment)
<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 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); ?>
By: <?php echo $comment->comment_author;?>

OK so lets walk through the code..


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,

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,


echo "<pre>";
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] =>
            [comment_author_url] =>
            [comment_author_IP] =>
            [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..

The following two tabs change content below.


Web Developer
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, Quad bikes, Downhill Mountain biking & Learning electronics using the Arduino.

Latest posts by Marty (see all)