如何通过帖子ID获取评论?


9

我有此自定义帖子查询,以列出特定类别内的所有帖子。例如我有这个:

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

因此,对于此页面,我想显示帖子列表以及随附的评论。我只显示每个帖子最多2条评论。

有内置的功能可以做到这一点吗?

Answers:


10

您可以使用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;
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.