get comments and get posts in loop

get_comments accepts an array of arguments, you are passing an integer.

If you want to retrieve all comments for a post use:

get_comments( array('post_id' => $post->ID, 'status' => 'approve') );

To get an already formatted comment list, is easier use the wp_list_comments() function, instead of another foreach cycle (code from codex):

  echo '<ol class="commentlist">';
  //Gather comments for a specific page/post 
  $comments = get_comments(array(
    'post_id' => $post->ID,
     'status' => 'approve'
  ));
  wp_list_comments(array(
    'per_page' => 10, // Allow comment pagination
     'reverse_top_level' => false //Show the latest comments at the top of the list
   ), $comments);
  echo '</ol>';

Leave a Comment