Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to show the last 5 comments for each post on my index page loop of posts. What Im using now is only showing the same comments for every post. How should I set this up and how can I add an avatar to each comment.

What have this obviously isn't working..

<?php
  $args = array(
'status' => 'approved',
'number' => '5',
   );
  $comments = get_comments($args);
    foreach($comments as $comment) :
  echo( $comment->get_avatar . $comment->comment_author . '<br />' . $comment->comment_content);
   endforeach;
 ?>
share|improve this question
    
If it's the same comment, it means it's the same context. So we would need the context your code snippet. –  Simon Mar 22 '12 at 20:19

1 Answer 1

up vote 2 down vote accepted

You should place your code inside the loop and add to the args array 'post_id' => get_the_ID() so it should look like this:

while(have_posts()){
   the_post();
   //your post loop output
   $args = array(
    'status' => 'approved',
    'number' => '5',
    'post_id' => get_the_ID()
    );
    $comments = get_comments($args);
    foreach($comments as $comment) :
        echo( $comment->get_avatar . $comment->comment_author . '<br />' . $comment->comment_content);
    endforeach;
}
share|improve this answer
    
Ohhh ok that works now..how do I get the avatar to display because it's not the way I have it. I need to control the size too. –  Pollux Khafra Mar 22 '12 at 20:28
    
change $comment->get_avatar to get_avatar($comment->comment_author_email,64) –  Bainternet Mar 22 '12 at 20:32
1  
Yeap! thanks for being awesome! –  Pollux Khafra Mar 22 '12 at 20:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.