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

   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>

效果图:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值