如题,给你的 WordPress 绘制一个评论统计吧!点击这里预览。代码比较简单,见下文,转自Leniy's BLOG。代码大概的原理是:首先读取数据库,依照不同目的进行数据库查询,然后把输出的数据格式化,最后调用 Google Chart Api 绘制图形(所以你不许翻墙,不然看不到演示)。
使用方法应该不必多言,在主题目录下新建个 php 文件,填入以上代码即可,当然,相关的样式你可以随便改改。然后新建-新建页面即可。另外值得一提的是:图表有时候可能不会正确绘制或者打不开,那是因为我大天朝的长城防火墙导致的,那还烦请您欢个墙试试~~
<?php
/**
* @author shephe.com
*/
/*
Template Name: Comments Chart Page
*/
?>
<?php get_header(); ?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
</br></br></br></br></br>
<h3><p style="text-align:center"><a href="https://www.shephe.com/2015/07/wordpress-comment-chart/" title="点此查看本页如何生成,图标不可见?被墙了-_-" target="_blank">本站活跃用户排名</a></p></h3>
<div id="chart_user_div" style="width: 1200px; height: 600px; margin: auto;"></div>
</br><hr/></br>
<h3><p style="text-align:center">评论数量图表</p></h3>
<div id="chart_day_div" style="width: 1200px; height: 600px; margin: auto;"></div>
</br><hr/></br>
<h3><p style="text-align:center">评论数量图表</p></h3>
<div id="chart_month_div" style="width: 1200px; height: 600px; margin: auto;"></div>
<?php
global $wpdb;
$numbers_day = 30;
$numbers_month = 12;
$numbers_user = 15;
$query_day ="SELECT COUNT(*) AS `cnt` , DATE_FORMAT( `comment_date` , '%Y-%m-%d' ) AS d FROM $wpdb->comments GROUP BY d ORDER BY `d` DESC LIMIT 0 , " . $numbers_day;
$query_month="SELECT COUNT(*) AS `cnt` , DATE_FORMAT( `comment_date` , '%Y-%m' ) AS d FROM $wpdb->comments GROUP BY d ORDER BY `d` DESC LIMIT 0 , " . $numbers_month;
$query_user ="
SELECT
COUNT( comment_author_email ) AS number,
comment_author_email,
comment_author
FROM (
SELECT *
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts
ON ( $wpdb->posts.ID = $wpdb->comments.comment_post_ID )
WHERE
comment_date > date_sub( NOW(), INTERVAL 365 DAY )
AND user_id = '0'
AND comment_approved = '1'
ORDER BY comment_ID DESC
) AS tempcmt
GROUP BY comment_author_email
ORDER BY number DESC
LIMIT {$numbers_user}";
$output_day = $wpdb->get_results($query_day);
$output_month = $wpdb->get_results($query_month);
$output_user = $wpdb->get_results($query_user);
?>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart_day);
google.setOnLoadCallback(drawChart_month);
google.setOnLoadCallback(drawChart_user);
function drawChart_day() {
var data = google.visualization.arrayToDataTable([
['date', 'comments'],
<?php foreach (array_reverse($output_day) as $o) {echo "['" . $o->d . "'," . $o->cnt . "],";} ?>
]);
var options = {
title: '每日评论数量'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_day_div'));
chart.draw(data, options);
}
function drawChart_month() {
var data = google.visualization.arrayToDataTable([
['date', 'comments'],
<?php foreach (array_reverse($output_month) as $o) {echo "['" . $o->d . "'," . $o->cnt . "],";} ?>
]);
var options = {
title: '每月评论数量'
};
var chart = new google.visualization.ColumnChart(document.getElementById('chart_month_div'));
chart.draw(data, options);
}
function drawChart_user() {
var data = google.visualization.arrayToDataTable([
['comment_author', 'comments'],
<?php foreach ($output_user as $o) {echo "['" . $o->comment_author . "'," . $o->number . "],";} ?>
]);
var options = {
title: '近一年内最活跃读者'
};
var chart = new google.visualization.PieChart(document.getElementById('chart_user_div'));
chart.draw(data, options);
}
</script>
</div>
</div>
<?php get_footer(); ?>
[…] 牧羊人:https://www.shephe.com/2015/07/wordpress-comment-chart […]