A-A+
给Ality主题添加网站统计小工具
以前鸟哥的hotnews主题,有个网站数据统计。换了Ality后,没有了,不习惯。
搜索了一下,找了代码,添加成功了。
自己没有一点html语言基础,完全是根据已有代码蒙的,搞定了,特来分享一下。
因为不懂代码,所以估计代码也不是全对,请专家指正。
在主题里的inc/functions/widgets.php,修改widgets.php文件。
在文件里最后,加入下面代码即可
- // 本站统计
- class recent_Tongji extends WP_Widget {
- function recent_Tongji() {
- $widget_ops = array('description' => '后来自己增加的本站统计小工具');
- $this->WP_Widget('recent_Tongji', '主题 本站统计', $widget_ops);
- }
- function form($instance){
- // 表单函数,控制后台显示
- // $instance 为之前保存过的数据
- // 如果之前没有数据的话,设置默认量
- $instance = wp_parse_args(
- (array)$instance,
- array(
- 'title' => '本站统计',
- 'establish_time' => '2013-01-27'
- )
- );
- /*
- $instance = wp_parse_args(
- (array)$instance,
- array(
- 'title' => '本站统计',
- 'establish_time' => '2007-03-06',
- 'post_count_no' => '1',
- 'draft_count_no' => '0',
- 'comment_count_no' => '2',
- 'establish_date_no' => '3',
- 'establish_time_no' => '4',
- 'tag_count_no' => '0',
- 'page_count_no' => '0',
- 'cat_count_no' => '0',
- 'link_count_no' => '0',
- 'user_count_no' => '0',
- 'last_update_no' => '5',
- 'support_me' => false
- )
- );
- */
- $title = htmlspecialchars($instance['title']);
- $establish_time = htmlspecialchars($instance['establish_time']);
- /*
- $post_count_no = htmlspecialchars($instance['post_count_no']);
- $draft_count_no = htmlspecialchars($instance['draft_count_no']);
- $comment_count_no = htmlspecialchars($instance['comment_count_no']);
- $establish_date_no = htmlspecialchars($instance['establish_date_no']);
- $establish_time_no = htmlspecialchars($instance['establish_time_no']);
- $tag_count_no = htmlspecialchars($instance['tag_count_no']);
- $page_count_no = htmlspecialchars($instance['page_count_no']);
- $cat_count_no = htmlspecialchars($instance['cat_count_no']);
- $link_count_no = htmlspecialchars($instance['link_count_no']);
- $user_count_no = htmlspecialchars($instance['user_count_no']);
- $last_update_no = htmlspecialchars($instance['last_update_no']);
- $support_me = $instance['support_me'];
- */
- // 表格布局输出表单
- $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>';
- /*
- $output .= '<br />输入数字1~11安排显示顺序,0表示不显示';
- $output .= '<table>';
- $output .= '<tr><td>日志总数:</td><td>';
- $output .= '<input id="'.$this->get_field_id('post_count_no') .'" name="'.$this->get_field_name('post_count_no').'" type="text" value="'.$instance['post_count_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>评论数目:</td><td>';
- $output .= '<input id="'.$this->get_field_id('comment_count_no') .'" name="'.$this->get_field_name('comment_count_no').'" type="text" value="'.$instance['comment_count_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>分类总数:</td><td>';
- $output .= '<input id="'.$this->get_field_id('cat_count_no') .'" name="'.$this->get_field_name('cat_count_no').'" type="text" value="'.$instance['cat_count_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>标签总数:</td><td>';
- $output .= '<input id="'.$this->get_field_id('tag_count_no') .'" name="'.$this->get_field_name('tag_count_no').'" type="text" value="'.$instance['tag_count_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>建站日期:</td><td>';
- $output .= '<input id="'.$this->get_field_id('establish_date_no') .'" name="'.$this->get_field_name('establish_date_no').'" type="text" value="'.$instance['establish_date_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>运行天数:</td><td>';
- $output .= '<input id="'.$this->get_field_id('establish_time_no') .'" name="'.$this->get_field_name('establish_time_no').'" type="text" value="'.$instance['establish_time_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>友链总数:</td><td>';
- $output .= '<input id="'.$this->get_field_id('link_count_no') .'" name="'.$this->get_field_name('link_count_no').'" type="text" value="'.$instance['link_count_no'].'" />';
- $output .= '</td></tr>';
- $output .= '<tr><td>最后更新:</td><td>';
- $output .= '<input id="'.$this->get_field_id('last_update_no') .'" name="'.$this->get_field_name('last_update_no').'" type="text" value="'.$instance['last_update_no'].'" />';
- $output .= '</td></tr>';
- $output .= '</table>';
- */
- /*
- $output .= '<label><input id="'.$this->get_field_id('support_me') .'" name="'.$this->get_field_name('support_me').'" type="checkbox" ';
- if ($instance['support_me']) {
- $output .= 'checked="checked"';
- }
- $output .= ' /> 支持开发者</label>';
- */
- 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']));
- /*
- $instance['post_count_no'] = strip_tags(stripslashes($new_instance['post_count_no']));
- $instance['draft_count_no'] = strip_tags(stripslashes($new_instance['draft_count_no']));
- $instance['comment_count_no'] = strip_tags(stripslashes($new_instance['comment_count_no']));
- $instance['establish_date_no'] = strip_tags(stripslashes($new_instance['establish_date_no']));
- $instance['establish_time_no'] = strip_tags(stripslashes($new_instance['establish_time_no']));
- $instance['tag_count_no'] = strip_tags(stripslashes($new_instance['tag_count_no']));
- $instance['page_count_no'] = strip_tags(stripslashes($new_instance['page_count_no']));
- $instance['cat_count_no'] = strip_tags(stripslashes($new_instance['cat_count_no']));
- $instance['link_count_no'] = strip_tags(stripslashes($new_instance['link_count_no']));
- $instance['user_count_no'] = strip_tags(stripslashes($new_instance['user_count_no']));
- $instance['last_update_no'] = strip_tags(stripslashes($new_instance['last_update_no']));
- */
- return $instance;
- }
- function widget($args, $instance){
- extract($args); //展开数组
- $title = apply_filters('widget_title',empty($instance['title']) ? ' ' : $instance['title']);
- $establish_time = empty($instance['establish_time']) ? '2013-01-27' : $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;
- $draft_posts = $count_posts->draft;
- $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'");
- $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
- $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'");
- // 显示数据
- /*
- $op = array(
- $published_posts,
- $draft_posts,
- $comments_count,
- $establish_time,
- $time,
- $count_tags,
- $page_posts,
- $count_categories,
- $link,
- $users,
- $last
- );
- $output = "";
- foreach ($instance as $key => $value){
- switch ($value){
- case "1":
- case "2":
- case "3":
- case "4":
- case "5":
- case "6":
- case "7":
- case "8":
- case "9":
- case "10":
- case "11":
- default: $output .= $this->efan_get_var_name($key, $op);break;
- }
- }
- */
- $output = '<li>日志总数:';
- $output .= $published_posts;
- $output .= ' 篇</li>';
- $output .= '<li>评论数目:';
- $output .= $comments_count;
- $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 efan_get_var_name($key, $op){
- extract($op);
- switch ($key){
- case "post_count_no": return '<li>日志总数:'.$published_posts.' 篇</li>';
- case "comment_count_no": return '<li>评论数目:'.$draft_posts.' 条</li>';
- case "establish_date_no": return '<li>建站日期:'.$establish_time.'</li>';
- case "establish_time_no": return '<li>运行天数:'.$time.' 天</li>';
- case "tag_count_no": return '<li>标签总数:'.$count_tags.' 个</li>';
- case "cat_count_no": return '<li>分类总数:'.$count_categories.' 个</li>';
- case "link_count_no": return '<li>友链总数:'.$link.' 个</li>';
- case "last_update_no": return '<li>最后更新:'.$last.'</li>';
- }
- }
- */
- }
- add_action( 'widgets_init', create_function( '', 'register_widget( "recent_Tongji" );' ) );
