限制评论数量 WordPress

Limit Comment Number Wordpress

以下是我用来循环显示帖子评论的代码(自定义 Post 类型)。我只想显示最新的 3 条评论。请帮我限制评论。

<?php  foreach (get_comments() as $comment): ?>
    <div><span class="author-name"><?php echo $comment->comment_author; ?> said:</span> <span class="author-cmnt">"<?php echo $comment->comment_content; ?>".</span></div>
<?php endforeach; ?>

您需要添加的唯一额外代码是 foreach 的限制器。 get_comments 可以通过数组提供一些参数,例如 number

<?php $args = array(
    'number'  => '3',
);

$comments = get_comments($args); ?>

<?php foreach ($comments as $comment) : ?>
  <div><span class="author-name"><?php echo $comment->comment_author; ?> said:</span> 
  <span class="author-cmnt">"<?php echo $comment->comment_content; ?>".</span></div>
<?php endforeach; ?>

这是我得到的输出: