^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云2核2g仅需86元/年

wordpress函数get_comments()用法示例

get_comments( string|array $args = '' )

Retrieve a list of comments.


描述

The comment list can be for the blog as a whole or for an individual post.

新用户2核2g仅需86元/年    领取1153套模板    免费cdn流量包    ¥3188礼包


参数

$args
(string|array)
(Optional)
Array or string of arguments. See WP_Comment_Query::parse_query() for information on accepted arguments.Default value: ”

返回值

(int|array) List of comments or number of found comments if $count argument is true.


源代码

File: wp-includes/comment.php

function get_comments( $args = '' ) {
	$query = new WP_Comment_Query;
	return $query->query( $args );
}

更新日志

Version 描述
2.7.0 Introduced.

相关函数

Uses

  • wp-includes/class-wp-comment-query.php:WP_Comment_Query::__construct()

Show comment counts of a post


$args = array(
	'post_id' => 1,   // Use post_id, not post_ID
        'count'   => true // Return only the count
);
$comments_count = get_comments( $args );
echo $comments_count;

Example


$comments = get_comments( array( 'post_id' => 15 ) );

foreach ( $comments as $comment ) :
	echo $comment->comment_author;
endforeach;

Show last 5 unapproved comments


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

Show comment counts of a user


$args = array(
	'user_id' => 1,   // Use user_id.
	'count'   => true // Return only the count.
);
$comments_count = get_comments( $args );

echo $comments_count;

Show comments of a user


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

Get comments from last 4 weeks


$args = array(
	'date_query' => array(
		'after' => '4 weeks ago',
		'before' => 'tomorrow',
		'inclusive' => true,
	),
);

$comments = get_comments( $args );
foreach ( $comments as $comment ) {
	// Output comments etc here
}

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈