admin管理员组

文章数量:1332889

Following is my code which I am using to display comments of posts in a loop (Custom Post Types). I would like to display only latest 3 comments. Kindly help me to limit comments.

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

Following is my code which I am using to display comments of posts in a loop (Custom Post Types). I would like to display only latest 3 comments. Kindly help me to limit comments.

<?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; ?>
Share Improve this question edited Jul 1, 2020 at 12:58 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Jul 1, 2020 at 10:35 AbirAbir 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

I don't usually work with comment so my suggestion is untested, but I see that get_comments() receives an array of args.

Try this:

$comments = get_comments(array("number" => 3))

and instead of your foreach loop:

foreach ($comments as $comment):

My answer to this is to use something like this:

$args = ['number' => 3];
$comments = get_comments( $args );

本文标签: Limit number of comments from getcomments()