如何在WordPress中获取帖子的最新评论?

如何在WordPress中获取帖子的最新评论?,wordpress,comments,Wordpress,Comments,我必须得到一篇帖子的最新评论。我是说,最近的评论。我尝试了以下代码。但它什么也不返回。它是空的 $args = array( 'number' => '1', 'post_id' => $post->ID ); $comments = get_comments($args); echo $comments->comment_content; 但是这篇文章有3条以上的评论。试试下面的代码: <?php $args = array(

我必须得到一篇帖子的最新评论。我是说,最近的评论。我尝试了以下代码。但它什么也不返回。它是空的

$args = array(  
    'number' => '1',
    'post_id' => $post->ID
);
$comments = get_comments($args);
echo $comments->comment_content; 
但是这篇文章有3条以上的评论。

试试下面的代码:

<?php 
    $args = array(
            'number' => '1',
            'post_id' => $post->ID
    );
    $comments = get_comments($args);
    foreach($comments as $comment) :
        echo $comment->comment_content;
    endforeach;
?>
试试这个:

<?php 
    $args = array(
        'post_id' => $post->ID,
        'orderby' => array('comment_date'),
        'order' => 'DESC',
        'number' => 1
    );
    $comment = get_comments( $args );
    echo $comment[0]->comment_content;
?>