如题,给你的Wordpress绘制一个评论统计吧!点击这里预览。代码比较简单,见下文,转自Leniy’s BLOG。代码大概的原理是:首先读取数据库,依照不同目的进行数据库查询,然后把输出的数据格式化,最后调用 Google Chart Api 绘制图形(所以你不许翻墙,不然看不到演示)。

使用方法应该不必多言,在主题目录下新建个 php 文件,填入以上代码即可,当然,相关的样式你可以随便改改。然后新建-新建页面即可。另外值得一提的是:图表有时候可能不会正确绘制或者打不开,那是因为我大天朝的长城防火墙导致的,那还烦请您欢个墙试试~~

<?php
/**
 * @author leniy
 */
/*
Template Name: Comments Chart Page
*/
?>
<?php get_header(); ?>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<h2><p style="text-align:center"><a href="http://www.youth-daily.com/wordpress%E8%AF%84%E8%AE%BA%E7%BB%9F%E8%AE%A1%E5%9B%BE%E8%A1%A8%E7%BB%98%E5%88%B6.html" title="点此查看本页如何生成,图标不可见?被墙了-_-" target="_blank"></a></p></h2><div id="chart_user_div" style="width: 640px; height: 400px; margin: auto;"></div></br>
<h2><p style="text-align:center"></p></h2><div id="chart_day_div" style="width: 640px; height: 500px; margin: auto;"></div></br>
<h2><p style="text-align:center"></p></h2><div id="chart_month_div" style="width: 640px; height: 500px; margin: auto;"></div>
    <?php
    global $wpdb;
    $numbers_day   = 30;
    $numbers_month = 9;
    $numbers_user  = 8;
    $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 180 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(); ?>