我们建立站点有一段时间之后,就可以在侧边栏显示站点的相关统计信息,比如站点已经运行多少天、发布了多少篇文章、有多少条评论,有多少阅读量等等。但是 WordPress 自身是不带有这个功能的,所以需要我们自己注册一个站点统计小工具,然后通过后台 – 外观 – 小工具 – 找到站点统计小工具,然后添加到相应的侧边栏即可。
注册站点统计小工具实现办法
1、新建一个 php 文件,并将以下代码添加到 sidebar_tongji.php 文件中。或者点此直接下载sidebar_tongji.php 文件(密码:dyme)。
- <?php
- // 站点统计小工具
- // 代码来自网络,版权无从考究
- // 定义小工具的类 EfanBlogStat
- class EfanBlogStat extends WP_Widget{
- function EfanBlogStat(){
- // 定义小工具的构造函数
- $widget_ops = array(‘classname’ => ‘widget_blogstat’, ‘description’ => ‘显示站点的统计信息’);
- $this->WP_Widget(false, ‘站点统计’, $widget_ops);
- }
- function form($instance){
- // 表单函数,控制后台显示
- // $instance 为之前保存过的数据
- // 如果之前没有数据的话,设置默认量
- $instance = wp_parse_args(
- (array)$instance,
- array(
- ‘title’ => ‘站点统计’,
- ‘establish_time’ => ‘2018-01-01’
- )
- );
- $title = htmlspecialchars($instance[‘title’]);
- $establish_time = htmlspecialchars($instance[‘establish_time’]);
- // 表格布局输出表单
- $output = ‘<table>’;
- $output .= ‘<tr><td>标题</td><td>’;
- $output .= ‘<input id=“‘.$this->get_field_id(‘title’) .'” name=“‘.$this->get_field_name(‘title’).'” type=“text” value=“‘.$instance[‘title’].'” />’;
- $output .= ‘</td></tr><tr><td>建站日期:</td><td>’;
- $output .= ‘<input id=“‘.$this->get_field_id(‘establish_time’) .'” name=“‘.$this->get_field_name(‘establish_time’).'” type=“text” value=“‘.$instance[‘establish_time’].'” />’;
- $output .= ‘</td></tr></table>’;
- echo $output;
- }
- function update($new_instance, $old_instance){
- // 更新数据的函数
- $instance = $old_instance;
- // 数据处理
- $instance[‘title’] = strip_tags(stripslashes($new_instance[‘title’]));
- $instance[‘establish_time’] = strip_tags(stripslashes($new_instance[‘establish_time’]));
- return $instance;
- }
- function widget($args, $instance){
- extract($args); //展开数组
- $title = apply_filters(‘widget_title’,empty($instance[‘title’]) ? ‘ ’ : $instance[‘title’]);
- $establish_time = empty($instance[‘establish_time’]) ? ‘2018-01-01’ : $instance[‘establish_time’];
- echo $before_widget;
- echo $before_title . $title . $after_title;
- echo ‘<ul>’;
- // $this->efan_get_blogstat($establish_time, $instance);
- $this->efan_get_blogstat($establish_time);
- echo ‘</ul>’;
- echo $after_widget;
- }
- function efan_get_blogstat($establish_time /*, $instance */){
- // 相关数据的获取
- global $wpdb;
- $count_posts = wp_count_posts();
- $published_posts = $count_posts->publish;
- $comments_count = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->comments”);
- $time = floor((time()-strtotime($establish_time))/86400);
- $count_tags = wp_count_terms(‘post_tag’);
- $count_pages = wp_count_posts(‘page’);
- $page_posts = $count_pages->publish;
- $count_categories = wp_count_terms(‘category’);
- $link = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = ‘Y'”);
- $last = $wpdb->get_results(“SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = ‘post’ OR post_type = ‘page’) AND (post_status = ‘publish’ OR post_status = ‘private’)”);
- $last = date(‘Y-n-j’, strtotime($last[0]->MAX_m));
- $total_views = $wpdb->get_var(“SELECT SUM(meta_value+0) FROM $wpdb->postmeta WHERE meta_key = ‘views'”);
- // 显示数据
- $output = ‘<li>日志总数:’;
- $output .= $published_posts;
- $output .= ‘ 篇</li>’;
- $output .= ‘<li>评论数目:’;
- $output .= $comments_count;
- $output .= ‘ 条</li>’;
- $output .= ‘<li>页面总数:’;
- $output .= $page_posts;
- $output .= ‘ 个</li>’;
- $output .= ‘<li>分类总数:’;
- $output .= $count_categories;
- $output .= ‘ 个</li>’;
- $output .= ‘<li>标签总数:’;
- $output .= $count_tags;
- $output .= ‘ 个</li>’;
- $output .= ‘<li>友链总数:’;
- $output .= $link;
- $output .= ‘ 个</li>’;
- $output .= ‘<li>浏览次数:’;
- $output .= $total_views;
- $output .= ‘ 次</li>’;
- $output .= ‘<li>建站日期:’;
- $output .= $establish_time;
- $output .= ‘</li>’;
- $output .= ‘<li>运行天数:’;
- $output .= $time;
- $output .= ‘ 天</li>’;
- $output .= ‘<li>最后更新:’;
- $output .= $last;
- $output .= ‘</li>’;
- echo $output;
- }
- }
- function EfanBlogStat(){
- // 注册小工具
- register_widget(‘EfanBlogStat’);
- }
- add_action(‘widgets_init’,’EfanBlogStat’);
- ?>
2、将步骤 1 的 sidebar_tongji.php 文件上传到当前主题文件夹内,并在当前主题的 functions.php 文件最后一个?>的前面添加一下代码
- //添加站点统计小工具文件
- include(“sidebar_tongji.php”);
请注意:如果 sidebar_tongji.php 文件不是放在主题根目录,那么第 2 步引用 sidebar_tongji.php 文件的路径就要做相应调整才行。比如是存放在主题文件的 inc 文件夹内,那么就应该是:
- //添加站点统计小工具文件
- include(“inc/sidebar_tongji.php”);
具体效果如下: