WordPress评论统计页面创建:显示四种方式(2)
来源:asp之家 时间:2010-03-26 10:18:00
接下来是“最新Pingbacks/Trackbacks”部分:
<h3>Recent Pingbacks / Trackbacks </h3>
<ul id="cc-recent-trackbacks">
<?php $sql = "SELECT c.*, p.post_title FROM $wpdb->comments c INNER JOIN
$wpdb->posts p ON (c.comment_post_id=p.ID)
WHERE comment_approved = '1' AND comment_type not in ('trackback','pingback')
ORDER BY comment_date DESC LIMIT $max"; $results = $wpdb->get_results($sql);
$template = '%g <a href="%au">%an</a> on <a href="%pu#comment-%cid">%pt</a>';
$echoed = 0; foreach ($results as $row){ $tags = array('%ct','%cd','%g','%pt','%pu','%au','%an','%cid');
$replacements = array($row->comment_title,$row->comment_date,get_avatar($row->comment_author_email,'32'),
$row->post_title, get_permalink($row->comment_post_ID),
$row->comment_author_url,
$row->comment_author,
$row->comment_ID); echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
$echoed=1; } if ($echoed==0) echo '<li>No comment found.</li>'; ?> </ul>
上述代码与“最新评论”中的代码非常相似,两者的区别在于,我们现在要查找的是comment_type中‘pingback’ / ‘trackback’下的评论,此外模板方面也有一些不同。 演示结果如下:
接下来是“评论次数最多的评论者”的代码:
<h3>Top Commenters</h3>
<ul id="cc-top-commenters">
<?php $sql = "SELECT comment_author, comment_author_url, comment_author_email, count(comment_ID)
as comment_count FROM $wpdb->comments WHERE comment_approved = '1'
AND comment_type not in ('trackback','pingback') GROUP BY comment_author, comment_author_url,
comment_author_email ORDER BY comment_count DESC LIMIT $max";
$results = $wpdb->get_results($sql);
$template = '<a href="%au">%g %an</a> (%c comments)';
$echoed = 0; foreach ($results as $row) { $tags = array('%g','%au','%an','%c');
$replacements = array(get_avatar($row->comment_author_email,'32'),
$row->comment_author_url,
$row->comment_author,
$row->comment_count); echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
$echoed = 1; } if ($echoed==0) echo '<li>No commenter found.</li>'; ?> </ul>
这里问题不大。 不过需要注意get_avatar()函数,当你指定一个电子邮件地址,这个函数会给出邮箱主人的Gravatar头像。 这种情况下我们就可以利用评论者的邮箱地址获取其头像图片。 之后利用一段类似于“最新评论”中的CSS代码:
#cc-top-commenters li { width: 100%; float: left; list-style-type: none; }
#cc-top-commenters li img { float: left; margin-top: -5px; }
得到结果如下:
最后是“被评论次数最多的文章”部分:
<h3>Most Commented Posts</h3>
<ul id="cc-most-comments">
$sql = "SELECT p.*, c.comment_count FROM
$wpdb->posts p INNER JOIN (SELECT comment_post_id, count(comment_ID) as comment_count
from $wpdb->comments
WHERE comment_approved='1' GROUP BY comment_post_id) c ON (c.comment_post_id=p.ID)
ORDER BY c.comment_count DESC LIMIT $max";
$results = $wpdb->get_results($sql);
$template = '<a href="%pu">%pt</a> (%c comments)';
$echoed = 0; foreach ($results as $row) {
$tags = array('%pd','%pt','%pu','%c');
$replacements = array($row->post_date,$row->post_title,get_permalink($row->ID),
$row->comment_count); echo '<li>' . str_replace($tags,$replacements,$template) . '</li>';
$echoed = 1; } if ($echoed==0)
echo '<li>No commenter found.</li>';
?>
</ul>
大功告成!
接下来我们给页面增加一个新版块——一些只有管理员可以看到的信息。