8

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.

Is there a built in function to do this?

8

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;

protected by Community Jul 20 '17 at 10:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

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