3

I need to get posts done by specific users and get also comments done to these posts, so far I can get the posts and comments but can't get how to do it in loop properly, because now I get the same comments displayed for each post no matter to which posts they actually belong to...

So here is the code...loop inside another one, I am now quite sure that is the way it should...

So the output in my case: list of posts + each posts get ALL the comments which have ever been done...

I want to get post + only its comments in a loop (I will put only php)

        <?php $posts = get_recent_posts_by_author_role('tenant');
                        foreach($posts as $post) {
                            $title=$post->post_title;
                            $perma_link=get_permalink($post->ID);
                            $img_post=get_the_post_thumbnail($post->ID);
                            $author_name=$post->post_author;
                            $content_post=$post->post_content;
                            $date=$post->post_date;
                            $content_style="comment_text"; 

                        ?>

<?php $comment=get_comments($post->ID);
                                foreach($comment as $com){

                                    $com_author=$com->comment_author;
                                    $com_date=$com->comment_date;
                                    $com_content=$com->comment_content;
                                    global $authordata;
                                    $author_roles=$authordata->roles;
                                    ?>

<?php  }?>
<?php  }?>
  • You want to attach the same comments to each and every post? Why not simply hard code them then? Btw, <?php ?> is no line break. Use it before starting PHP code and close it afterwards ?> again. – kaiser Sep 29 '13 at 5:36
5

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>';
0

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>';

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.