0

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;
 ?>
  • 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
3

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;
}
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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