WordPress获取指定最新评论列表函数方法

get_comments()是可以在主循环外获取最新评论列表的WordPress函数,通过get_comments()函数可以获取整站的最新评论,通过设置相关参数还可以实现获取指定文章、指定用户、指定ID或指定邮箱的评论,get_comments()函数可以返回评论ID、评论的文章ID、评论用户、评论邮箱、评论内容等信息。比如要在首页调用文章ID为1的评论,通过该函数就可以轻易实现。

函数代码


'',
'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 );
?>

参数说明


$author_email – (字符串)评论用户的邮件地址,默认为空。
$author__in – (数组)包含指定用户ID的评论,默认为空。
$author__not_in – (数组)排除指定用户ID的评论,默认为空。
$comment__in – (数组)获取指定ID的评论,默认为空。
$comment__not_in – (数组)排除指定ID的评论,默认为空。
$count – (布尔型)返回评论的数量值,默认为false。
$date_query – (数组)返回限制时间段的评论,或参考WP_Date_Query,默认为空。
$fields – (字符串)返回评论的字段,仅限注释ID的“ids”或空,默认为空。
$ID – (整型)暂未被使用?(官网注释Currently unused.),默认为空。
$include_unapproved – (数组)包含未被审核的评论ID或邮箱地址,默认为空。
$karma – (整型)用于检索匹配注释的KARMA得分,默认为空。
$meta_key – (字符串)包含的评论自定义字段,默认为空。
$meta_value – (字符串)包含的评论自定义字段值,必须和$meta_key一起使用,默认为空。
$meta_query – (数组)自定义字段查询条件,参考WP_Meta_Query函数,默认为空。
$number – (整型)返回的评论条数,不设置则不限量数量,默认为空。
$offset – (整型)从第几条开始的评论,默认为空。
$orderby – (字符串数组)返回的评论排序方式,如果根据“meta_value”或“meta_value_num”排列,那么必须设置$meta_key参数;如果使用meta_query查询,那么必须使用它的数组值。支持的排序方式:comment_agent、comment_approved、comment_author、comment_author_email、comment_author_IP、comment_author_url、comment_content、comment_date、comment_date_gmt、comment_ID、comment_karma、comment_parent、comment_post_ID、comment_type、user_id、meta_value、meta_value_num、the value of $meta_key、and the array keys of $meta_query、Also accepts false, an empty array, or ‘none’ to disable ORDER BY clause,默认为comment_date_gmt。
$order – (字符串)排序顺序,可选ASC或DESC,默认DESC。
$parent – (整型)指定ID的子评论,默认为空。
$post_author__in – (数组)获取指定文章作者ID的评论,默认为空。
$post_author__not_in – (数组)排除指定文章作者ID的评论,默认为空。
$post_ID – (整型)(官网注释Currently unused),默认为空。
$post_id – (整型)指定单篇文章ID,默认为空。
$post__in – (数组)指定多篇文章ID的评论,默认为空。
$post__not_in – (数组)排除指定文章ID的评论,默认为空。
$post_author – (整型)指定文章作者ID的评论,默认为空。
$post_status – (字符串)文章的状态,草稿、待审或者已发布,默认为空。
$post_type – (字符串)文章类型,如post、page或自定义文章类型,默认为空。
$post_name – (字符串)文章名称,默认为空。
$post_parent – (整形)指定父ID的评论,默认为空。
$search – (字符串)搜索匹配,默认为空。
$status – (字符串)评论的状态,支持“hold”、“approve”、“all”或自定义的评论状态,默认为all。
$type – (字符串数组)评论的类型,可选“comment”、“pings”(包含“pingback”和“trackback”),或自定义类型,默认为空。
$type__in – (数组)评论的类型,多个类型使用,默认为空。
$type__not_in – (数组)要排除的评论类型,默认为空。
$user_id – (整型)指定会员ID的评论。

函数返回值

注:函数的返回值是数组,数组包含以下字段。

comment_ID – 评论ID
comment_post_ID – 评论父ID
comment_author – 评论用户名
comment_author_email – 评论用户邮箱
comment_author_url – 评论用户网址
comment_author_IP – 评论用户IP
comment_date – 评论时间,格式(YYYY-MM-DD HH:MM:SS)
comment_date_gmt – 评论的GMT时间(YYYY-MM-DD HH:MM:SS)
comment_content – 评论内容
comment_karma – 评论的karma
comment_approved – 评论状态(0,1或“spam”)
comment_agent – 评论用户的工具(浏览器、操作系统等信息)
comment_type – 评论的类型(pingback或trackback),空的话表示常规评论
comment_parent – 嵌套评论的父评论(0为顶层)
user_id – 用户ID(如果评论的用户是网站注册用户则返回)

一、调用文章ID为15的所有评论


comment_author);
endforeach;
?>

二、调用文章ID为1的5条未审核的评论


'hold',
'number' => '5',
'post_id' => 1, // use post_id, not post_ID
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo($comment->comment_author . '
' . $comment->comment_content);
endforeach;
?>

三、显示文章ID为1的评论的数量


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

四、调用注册ID为1的用户评论的数量


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

五、调用注册ID为1的用户的评论


1, // use user_id

);
$comments = get_comments($args);
foreach($comments as $comment) :
echo($comment->comment_author . '
' . $comment->comment_content);
endforeach;
?>

六、调用最近四周的评论


array(
'after' => '4 week ago',
'before' => 'tomorrow',
'inclusive' => true,
),
);
$posts = get_comments($args);
foreach ($posts as $post) {
// Output comments etc here
}
?>

七、删除同一篇文章同一个人的重复评论


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

业界动态

网站开通CND后如何判断CDN是否生效

2018-9-15 12:58:16

业界动态

删除手机UC浏览器强制插入关键词链接的方法

2018-9-15 14:00:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索