WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

zh-cn:函数参考/get comments

本文已被标记为未完成状态。您可以将其补充或翻译完整,以此帮助完善 Codex。

说明

Retrieve a list of comments.

用法

<?php get_comments$args ); ?>

默认用法

<?php $defaults = array(
	'author_email' => '',
	'ID' => '',
	'karma' => '',
	'number' => '',
	'offset' => '',
	'orderby' => '',
	'order' => 'DESC',
	'parent' => '',
	'post_id' => '',
	'post_author' => '',
	'post_name' => '',
	'post_parent' => '',
	'post_status' => '',
	'post_type' => '',
	'status' => '',
	'type' => '',
	'user_id' => '',
	'search' => '',
	'count' => false
); ?>

参数

$status
(string) (optional) Only return comments with this status.
  • 'hold' - unapproved comments
  • 'approve' - approved comments
  • 'spam' - spam comments
  • 'trash' - trash comments
Default: None
$orderby
(string) (optional) Set the field used to sort comments.
Default: comment_date_gmt
$order
(string) (optional) How to sort $orderby. Valid values:
  • 'ASC' - Ascending (lowest to highest).
  • 'DESC' - Descending (highest to lowest).
Default: DESC
$number
(integer) (optional) Number of comments to return. Leave blank to return all comments.
Default: unlimited
$offset
(integer) (optional) Offset from latest comment. You must include $number along with this.
Default: 0
$post_id
(integer) (optional) Only return comments for a particular post or page.
Default: None
$user_id
(integer) (optional) Only return comments for a particular user.
Default: None
$count
(integer) (optional) Only return the total count of comments.
Default: None

返回

(Array) 
Comment fields with the following index keys (or an empty array if there are no comments):
comment_ID 
(integer) The comment ID
comment_post_ID 
(integer) The ID of the post/page that this comment responds to
comment_author 
(string) The comment author's name
comment_author_email 
(string) The comment author's email
comment_author_url 
(string) The comment author's webpage
comment_author_IP 
(string) The comment author's IP
comment_date 
(string) The datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_date_gmt 
(string) The GMT datetime of the comment (YYYY-MM-DD HH:MM:SS)
comment_content 
(string) The comment's content
comment_karma 
(integer) The comment's karma
comment_approved 
(string) The comment approval level (0, 1 or "spam")
comment_agent 
(string) The commenter's user agent (browser, operating system, etc.)
comment_type 
(string) The comment's type if meaningfull (pingback|trackback), empty for normal comments
comment_parent 
(string) The parent comment's ID for nested comments (0 for top level)
user_id 
(integer) The comment author's ID if s/he is registered (0 otherwise)

示例

<?php 
$comments = get_comments('post_id=15');
foreach($comments as $comment) :
	echo($comment->comment_author);
endforeach;
?>

Show last 5 unapproved comments:

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

Show comment counts of a post:

<?php
$args = array(
	'post_id' => 1, // use post_id, not post_ID
        'count' => true //return only the count
);
$comments = get_comments($args);
echo $comments

?>


Show comment counts of a user:

<?php
$args = array(
	'user_id' => 1, // use user_id
        'count' => true //return only the count
);
$comments = get_comments($args);
echo $comments

?>

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() is located in wp-includes/comment.php.

相关

See also index of Function Reference and index of Template Tags.