我有这个自定义的帖子查询来列出特定类别中的所有帖子。例如,我有:
$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条评论。
是否有内置功能来执行此操作?
最合适的回答,由SO网友:Evan Yeung 整理而成
您可以使用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;