简体   繁体   English

WordPress获得最新评论

[英]wordpress get recent comments

I have been tryin for the last few hours to research how to get recent comments from wordpress. 在过去的几个小时中,我一直在尝试研究如何从wordpress获得最新评论。 here is how I managed to get recent posts.... 这是我设法获得最新帖子的方法。

    <h4>Recent Posts</h4>
    <ul>
        <?php
            $args = array( 'numberposts' => '5' );
            $recent_posts = wp_get_recent_posts( $args );
            foreach( $recent_posts as $recent ){
                echo '<li><a href="' . get_permalink($recent["ID"]) . '">' .   $recent["post_title"].'</a> </li> ';
            }
            wp_reset_query();
        ?>
    </ul>

how do I get the latest comments.. 我如何获得最新评论。

ps. ps。 I have tried to change the posts to comments and doesn't work. 我试图将帖子更改为评论,但不起作用。

Thanks in advance 提前致谢

Steven 史蒂文

You can retrieve recent comments using get_comments() . 您可以使用get_comments()检索最近的评论。

get_comments() works in a very similar way to the function you're using to retrieve posts. get_comments()工作方式与您用来检索帖子的功能非常相似。

<?php $recent_comments = get_comments( array( 
    'number'      => 5, // number of comments to retrieve.
    'status'      => 'approve', // we only want approved comments.
    'post_status' => 'publish' // limit to published comments.
) );

if ( $recent_comments ) {
    foreach ( (array) $recent_comments as $comment ) {

        // sample output - do something useful here
        echo '<a href="' . esc_url( get_comment_link( $comment ) ) . '">' . get_the_title( $comment->comment_post_ID ) . '</a>';

    }
} 

Further reading: https://codex.wordpress.org/Function_Reference/get_comments 进一步阅读: https : //codex.wordpress.org/Function_Reference/get_comments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM