WordPress 评论统计图表绘制

WordPress 评论统计图表绘制

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

WordPress 评论统计图表绘制
WordPress 评论统计图表绘制

使用方法应该不必多言,在主题目录下新建个 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(); ?>
1920 1342 Kevin's
「WordPress 评论统计图表绘制」有 11 条评论
  • […] 牧羊人:https://www.shephe.com/2015/07/wordpress-comment-chart […]

  • 成人之美
    05/05/2020 at 21:13 回复

    这个真不错。去试试。

  • NiCE
    05/05/2020 at 16:18 回复

    这个可视化太酷了!!

  • Lvtu
    03/24/2020 at 13:14 回复

    jsapi被墙了,可以换成这个:https://www.gstatic.com/charts/loader.js,或者保存到本地!

  • 08/04/2015 at 07:54 回复

    我去,还可以这样,涨姿势了

  • 老杨
    08/03/2015 at 17:04 回复

    要翻墙有时候也挺懒的,虽然不难……

    • 的头像
      牧羊人
      08/10/2015 at 22:20 回复

      时时刻刻保持翻墙的姿态,ahh

  • 让心情去旅行
    08/03/2015 at 16:53 回复

    我早上看到一次 我还纳闷这个是怎么做的呢!
    下午就看到这篇文章了

    • 的头像
      牧羊人
      08/10/2015 at 22:10 回复

      以前我 Finle 一个月一千多评论,我都不知道怎么来的~~所以搞了这么个东西,现在用不着了,一个月没几条评论~

  • itorr
    07/30/2015 at 11:27 回复

    这个好厉害!这是按照文章分色的么

发表评论

请输入关键词…