get_comments()是可以在主循环外获取最新评论列表的WordPress函数,通过get_comments()函数可以获取整站的最新评论,通过设置相关参数还可以实现获取指定文章、指定用户、指定ID或指定邮箱的评论,get_comments()函数可以返回评论ID、评论的文章ID、评论用户、评论邮箱、评论内容等信息。比如要在首页调用文章ID为1的评论,通过该函数就可以轻易实现。
1 | <?php get_comments( $args ); ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <?php $args = array( 'author_email' => '', 'author__in' => '', 'author__not_in' => '', 'include_unapproved' => '', 'fields' => '', 'ID' => '', 'comment__in' => '', 'comment__not_in' => '', 'karma' => '', 'number' => '', 'offset' => '', 'orderby' => '', 'order' => 'DESC', 'parent' => '', 'post_author__in' => '', 'post_author__not_in' => '', 'post_ID' => '', // ignored (use post_id instead) 'post_id' => 0, 'post__in' => '', 'post__not_in' => '', 'post_author' => '', 'post_name' => '', 'post_parent' => '', 'post_status' => '', 'post_type' => '', 'status' => 'all', 'type' => '', 'type__in' => '', 'type__not_in' => '', 'user_id' => '', 'search' => '', 'count' => false, 'meta_key' => '', 'meta_value' => '', 'meta_query' => '', 'date_query' => null, // See WP_Date_Query ); get_comments( $args ); ?> |
注:函数的返回值是数组,数组包含以下字段。
1 2 3 4 5 6 | <?php $comments = get_comments('post_id=15'); foreach($comments as $comment) : echo($comment->comment_author); endforeach; ?> |
1 2 3 4 5 6 7 8 9 10 11 | <?php $args = array( 'status' => 'hold', 'number' => '5', 'post_id' => 1, // use post_id, not post_ID ); $comments = get_comments($args); foreach($comments as $comment) : echo($comment->comment_author . '<br />' . $comment->comment_content); endforeach; ?> |
1 2 3 4 5 6 7 8 | <?php $args = array( 'post_id' => 1, // use post_id, not post_ID 'count' => true //return only the count ); $comments = get_comments($args); echo $comments ?> |
1 2 3 4 5 6 7 8 | <?php $args = array( 'user_id' => 1, // use user_id 'count' => true //return only the count ); $comments = get_comments($args); echo $comments ?> |
1 2 3 4 5 6 7 8 9 10 | <?php $args = array( 'user_id' => 1, // use user_id ); $comments = get_comments($args); foreach($comments as $comment) : echo($comment->comment_author . '<br />' . $comment->comment_content); endforeach; ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 | <?php $args = array( 'date_query' => array( 'after' => '4 week ago', 'before' => 'tomorrow', 'inclusive' => true, ), ); $posts = get_comments($args); foreach ($posts as $post) { // Output comments etc here } ?> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // get all approved comments with empty number arg $all_comments=get_comments( array('status' => 'approve', 'number'=>'') ); // array to hold comment ids that are dupes $comment_ids_to_delete=array(); foreach($all_comments as $k=>$c) { $kk=($k-1); // the previous comments index in all_comments array $pc=$all_comments[$kk]; // the previous comment object // if comment authors the same, and comment_content the same add to deletions array if($pc->comment_author == $c->comment_author && $pc->comment_content == $c->comment_content) { $comment_ids_to_delete[]=$pc->comment_ID; // previous comment id } } // delete the comment by id foreach($comment_ids_to_delete as $k=>$v): wp_delete_comment($v); endforeach; ?> |
wp-includes/comment.php
官方文档:https://codex.wordpress.org/Function_Reference/get_comments