使用这个代码修改下即根据文章ID获取这篇文章的所有评论者的
//获取最新评论 function get_recent_comments(){ // 不显示pingback的type=comment,不显示自己的,user_id=0.(此两个参数可有可无) $comments=get_comments(array(</code><code class="language-php">'post_id' => 0,</code><code class="language-php">'number'=>5,'status'=>'approve','type'=>'comment','user_id'=>0)); //获取指定文章只需要添加参数数组中
post_id的值
$output = ''; foreach($comments as $comment) { $random = mt_rand(1, 10); //去除评论内容中的标签 $comment_content = strip_tags($comment->comment_content); //评论可能很长,所以考虑截断评论内容,只显示10个字 $short_comment_content = trim(mb_substr($comment_content ,0, 10,"UTF-8")); //先获取头像 $output .= '<li><div style="float:left;"><img style="padding-right:5px;width:55px;height:45px;" alt="nopic" title="no pic i say a jb" src=" '.get_bloginfo("template_url").'/images/nopic/nopic'.$random.'.gif"></div> '; //获取作者 $output .= '<div style="margin-left: 46px;color:red">'.$comment->comment_author .':</div><div style="margin-left:46px;"><a href="'; //评论内容和链接 $output .= get_permalink( $comment->comment_post_ID ) .'" title="查看 '.get_post( $comment->comment_post_ID )->post_title .'">'; $output .= $short_comment_content .'..</a></div></li>'; } //输出 echo $output; }
使用函数
get_comments
此函数官方文档 http://codex.wordpress.org.cn/Function_Reference/get_comments
函数默认方法数组
<?php $defaults = array(
'author_email' => '',
'ID' => '',
'karma' => '',
'number' => '',
'offset' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
'post_id' => 0,
'post_author' => '',
'post_name' => '',
'post_parent' => '',
'post_status' => '',
'post_type' => '',
'status' => '',
'type' => '',
'user_id' => '',
'search' => '',
'count' => false,
'meta_key' => '',
'meta_value' => '',
'meta_query' => '',
); ?>
返回值
(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)