How to get comments by post ID?

I have this custom post query to list all the posts within a specific category. For example I have this:

$args = array('cat' => 'home','post_type' => 'post'));
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
 // do stuff here
endwhile;

So for this page I would like to show the list of posts but also the accompanying comments. I am only showing maximum 2 comments for each post.

Read More

Is there a built in function to do this?

Related posts

Leave a Reply

1 comment

  1. You can use get_comments. Function Reference/get comments

    $args = array('cat' => 'home','post_type' => 'post'));
    $post_obj = new WP_Query($args);
    while($post_obj->have_posts() ) : $post_obj->the_post();
        //display comments
        $comments = get_comments(array(
            'post_id' => $post->ID,
            'number' => '2' ));
        foreach($comments as $comment) {
            //format comments
        }
    endwhile;