WordPress模板评论函数:get_comments()获取评论信息
wordpress提供了非常多的功能强大的函数,本站将介绍wordpress模板的评论函数——get_comments()函数。get_comments()函数可以获取整站的评论,在设置相关参数后,还可以获取指定文章、指定用户、指定评论ID或指定邮箱的评论。在wordpress主题开发中,通过get_comments()函数,可以返回评论的很多信息,如:评论ID、评论的文章ID、评论用户、评论邮箱、评论内容等。
get_comments()语法:
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 );
get_comments()函数只有一个参数,这个参数是一个数组(如上)。下面是这个数组中的所有参数的详解:
$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 – (字符串数组)返回的评论排序方式,$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的评论。
get_comments()函数会返回一系列评论信息,函数的返回值是数组,数组包含以下字段:
comment_ID – 评论IDcomment_post_ID – 评论父IDcomment_author – 评论用户名comment_author_email – 评论用户邮箱comment_author_url – 评论用户网址comment_author_IP – 评论用户IPcomment_date – 评论时间,格式(YYYY-MM-DD HH:MM:SS)comment_date_gmt – 评论的GMT时间(YYYY-MM-DD HH:MM:SS)comment_content – 评论内容comment_karma – 评论的karmacomment_approved – 评论状态(0,1或“spam”)comment_agent – 评论用户的工具(浏览器、操作系统等信息)comment_type – 评论的类型(pingback或trackback),空的话表示常规评论comment_parent – 嵌套评论的父评论(0为顶层)user_id – 用户ID(如果评论的用户是网站注册用户则返回)
如果是要自定义评论模板时,这个get_comments()函数会派上大大的用场,可以最大地实现评论列表的自由度。下面是一个获取指定用户的所有评论列表案例:
<h4>你的评论</h4><ul><?php$args = array(‘user_id’ => $users->ID, // use user_id‘count’ => true);$counts = get_comments($args); //获取评论数量$page_num2 = 1; //每页显示多少条$page_count2 = ceil($counts/$page_num2); //总页数$page_now2 = $_GET[‘page_now2’] ?: 1; //当前页$start2 = ($page_now2-1)*$page_num2; //起$args = array(‘user_id’ => $users->ID, //指定用户的评论);$comments = get_comments($args);for($i=$start2; $i<($start2+$page_num2); $i++){?><li><div class=”comments listing-reviews”><ul><li><div class=”avatar”><?php echo get_avatar($users->ID,40);//用户图像 ?></div><div class=”comment-content”><div class=”arrow-comment”></div><div class=”comment-by”><div class=”comment-by-listing own-comment”>你对 <?php echo get_comment_author( $comments[$i]->comment_parent); ?> 的评论</div><span class=”date”><?php echo $comments[$i]->comment_date; ?></span><div class=”star-rating” data-rating=”4.5″></div></div><p><?php echo $comments[$i]->comment_content; ?></p><a href=”#small-dialog2″ class=”rate-review edit_cur_comment” id=”<?php echo $comments[$i]->comment_ID ; ?>”><i class=”sl sl-icon-note”></i>编辑</a></div></li></ul></div></li><?php }?></ul></div><!– 分页 –><div class=”clearfix”></div><div class=”pagination-container margin-top-30 margin-bottom-0″><nav class=”pagination”><ul><?phpfor( $i=1; $i<($page_count2+1); $i++ ){if($i==$page_now2){echo ‘<li><a href=”javascript:;” class=”current-page”>’.$i.'</a></li>’;}else{echo ‘<li><a href=”‘.home_url().’/?author=’.$users->ID.’&act=comment_list&page_now2=’.$i.'”>’.$i.'</a></li>’;}}?></ul></nav></div>