WordPress添加侧栏小工具-博客统计(网站统计)

news/2024/7/6 23:31:25

   WordPress侧边栏“博客统计”小工具的制作方法。首先要下载cztcms.zip文件,解压得到一个PHP文件。蓝奏云地址:▶ cztcms.zip

1、将这个PHP文件放到主题目录下。打开主题目录下的function.php,在最后一个 ?> 前插入以下代码:

include("cztcms.php");

到“后台——外观——小工具”中添加“博客统计”小工具即可。默认是这样的:

2、由于原来的样式太丑了,我经过优化以后是这样的。我只用了其中的文章数和阅读数。其他的按照我的代码样式自己添加即可。

优化后的代码,将以下代码替换cztcms.php里面的代码即可。或者也可以自己新建一个cztcms.php文件,然后复制进去。以下代码是我的改造结果。你也可以自己去创造。

<?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' => '2020-02-02'
			)
		);
		$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>';  
		$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('draft_count_no') .'" name="'.$this->get_field_name('draft_count_no').'" type="text" value="'.$instance['draft_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('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('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('page_count_no') .'" name="'.$this->get_field_name('page_count_no').'" type="text" value="'.$instance['page_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('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('user_count_no') .'" name="'.$this->get_field_name('user_count_no').'" type="text" value="'.$instance['user_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']));
	
		return $instance;
	}
	function widget($args, $instance){
		extract($args); //展开数组
		$title = apply_filters('widget_title',empty($instance['title']) ? ' ' : $instance['title']);
		$establish_time = empty($instance['establish_time']) ? '2020-02-02' : $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'");  
		$counter = intval(file_get_contents("counter.dat"));  
    if(!$_SESSION['#'])  
    {  
     $_SESSION['#'] = true;  
     $counter++;  
     $fp = fopen("counter.dat","w");  
     fwrite($fp, $counter);  
     fclose($fp);  
	}  
		$output ='<table>';
		$output .= '<tr style="text-align:center;font-size:18px; font-family:黑体" >';
		$output .= '<td style="width:150px;">文章总数</td>';
		$output .= '<td style="width:150px;">访问总数</td>';
		$output .= '<td style="width:150px;">阅读总数</td>';
		$output .='</tr>';
		$output .='<br>';
		$output .='<tr style="text-align:center;font-size:16px; font-family:Consolas;">';
		$output .='<td style="width:150px;">';
		$output .='<a style="color:DodgerBlue;">';
		$output .= $published_posts;
		$output .='</a>';
		$output .=' 篇';
		$output .='</td>';
		$output .='<td style="width:150px;">';
		$output .='<a style="color:DodgerBlue;">';
		$output .= $counter;
		$output .='</a>';
		$output .=' 次';
		$output .='</td>';
		$output .='<td style="width:150px;">';
		$output .='<a style="color:DodgerBlue;">';
		$output .= $total_views;
		$output .='</a>';
		$output .=' 次';
		$output .='</td>';
		$output .='</tr>';
		$output .= '</table>';
		$output .= '<hr>';
		$output .='<p style="text-align:center;font-size:24px; font-family:Consolas;">';
		$output .='<a style="color:red;">♥ </a>';
		$output .='<a style="color:DodgerBlue;">2020-0202</a>';
		$output .='<a style="color:red;"> ♥</a>';
		$output .='</p>';
		if (is_user_logged_in()){
		}
		if (get_option("users_can_register") == 1){
		}
		echo $output;
	}
	
}
function EfanBlogStat(){
	// 注册小工具
	register_widget('EfanBlogStat');
}
add_action('widgets_init','EfanBlogStat');
?>

3、中间的访问总数是自己写的代码,其实就是PHP统计网站访问次数的一个方法(刷新一次加一),上面的改造已经将这个功能融合,也可以单独使用。以下代码添加到需要显示的地方即可。会自动在站点根目录下新建一个www.dat文件,用来存放访问次数。具体代码如下。

<?php
    @session_start();  
    $counter = intval(file_get_contents("www.dat"));  
    if(!$_SESSION['#'])  
    {  
     $_SESSION['#'] = true;  
     $counter++;  
     $fp = fopen("www.dat","w");  
     fwrite($fp, $counter);  
     fclose($fp);  
    }  
 ?>
<p align="center">您是到访的第<?php echo "$counter";?>位用户</p>

4.再分享一段代码,可以显示网站运行天数实时更新精确到秒
代码:在需要显示的地方加入即可:

博客稳定运行 
<SPAN id=span_dt_dt style="color: #0196e3;"></SPAN>
<SCRIPT language=javascript>
function show_date_time(){
window.setTimeout("show_date_time()", 1000);
BirthDay=new Date("02/02/2020 00:00:00");//这个日期是建站日期
today=new Date();
timeold=(today.getTime()-BirthDay.getTime());
sectimeold=timeold/1000
secondsold=Math.floor(sectimeold);
msPerDay=24*60*60*1000
e_daysold=timeold/msPerDay
daysold=Math.floor(e_daysold);
e_hrsold=(e_daysold-daysold)*24;
hrsold=Math.floor(e_hrsold);
e_minsold=(e_hrsold-hrsold)*60;
minsold=Math.floor((e_hrsold-hrsold)*60);
seconds=Math.floor((e_minsold-minsold)*60);
span_dt_dt.innerHTML=""+daysold+"天"+hrsold+"小时"+minsold+"分"+seconds+"秒";
}
show_date_time();
</SCRIPT>

效果图:


http://www.niftyadmin.cn/n/3649403.html

相关文章

every 和some_如何使用.every()和.some()操纵JavaScript数组

every 和some介绍 (Introduction) ES5 and ES6 brought many changes to JavaScript, including better ways of working with a collection of data expressed as arrays. While the language has significantly improved support for declarative data manipulation on array…

PhotoView的使用

1. 功能介绍 特性(Features)&#xff1a; 支持Pinch手势自由缩放。支持双击放大/还原。支持平滑滚动。在滑动父控件下能够运行良好。&#xff08;例如&#xff1a;ViewPager&#xff09;支持基于Matrix变化&#xff08;放大/缩小/移动&#xff09;的事件监听。 优势&#xff1…

PackageManagerService概述

PackageManagerService主要负责对系统的apk进行管理&#xff0c;不管是系统apk(/system/app)&#xff0c;还是我们手工安装上去的&#xff0c;系统所有的apk都是由其管理的。 我们看一下PackageManager类图 从图可知&#xff0c;PackageManage负责提供对外的接口&#xff0c;Pa…

PHP+MySQL实现精确统计网站访问量(IP个数)

基于WordPress的网站有很多统计功能。但是只能统计文章阅读数。不能统计访客人数。以下代码可以实现获取来访用户的IP地址&#xff0c;一个IP对应一次访问。即使刷新也不会增加访问量。这个非常精确。 1、创建一个存储数据的表。进入MySQL后直接创建即可。 create table wp_jc_…

JDBC连接sql server 2008数据库

public class TestJdbc {public static void main(String[] args) {// 定义需要的对象PreparedStatement ps null;Connection ct null;ResultSet rs null;try {// 初始化我们的对象// 1.加载驱动// Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"…

如何使用React-Lottie将动画添加到React应用

介绍 (Introduction) Adobe After Effects can be an intuitive way to generate web animations, but there has historically been problems converting these animations to web apps. It is usually necessary to use a third party program to import an animation from A…

Android系统常用隐藏命令大全

本文出自&#xff1a;http://androidkaifa.com/thread-1038-1-1.html android手机隐藏命令大全&#xff0c;用之前要考虑清楚尤其是涉及到格式化或者恢复出厂设置类的&#xff01; 注意&#xff1a;因Android版本较多&#xff0c;固有部分隐藏命令或不能使用&#xff08;笔者用…

WordPress文本工具实现代码执行功能

WordPress自带有两个小工具可以执行代码功能。一个是自定义HTML小工具&#xff0c;不过只能运行HTML、CSS、JavaScript三种语言。文本小工具默认是不可以运行代码的。要实现这个功能&#xff0c;需要添加以下代码。 使用方法&#xff1a;在当前主题目录下面的functions.php里面…