WordPress免插件统计网站访问量
效果如本站底部,转载了网络上常用的方法,不用安装第三方插件,纯是代码实现
1.在主题编辑的function.php 函数中添加如下代码
/**
* 全站总访问量/今日总访问量/当前是第几个访客
* @return [type] [description]
*/
function wb_site_count_user(){
$addnum = 1;
session_start();
$date = date('ymd',time());
if(!isset($_SESSION['wb_'.$date]) && !$_SESSION['wb_'.$date]){
$count = get_option('site_count');
if(!$count || !is_array($count)){
$newcount = array(
'all' => 12012,
'date' => $date,
'today' => $addnum
);
update_option( 'site_count', $newcount );
}else{
$newcount = array(
'all' => ($count['all']+$addnum),
'date' => $date,
'today' => ($count['date'] == $date) ? ($count['today']+$addnum) : $addnum
);
update_option( 'site_count', $newcount );
}
$_SESSION['wb_'.$date] = $newcount['today'];
}
return;
}
add_action('init', 'wb_site_count_user');
//输出访问统计
function wb_echo_site_count(){
session_start();
$sitecount = get_option('site_count');
$date = date('ymd',time());
echo '<p>总访问量:<span style="color:white">'.absint($sitecount['all']).'</span> 今日访问量:<span style="color:white">'.absint($sitecount['today']).'</span> 您是今天第:<span style="color:white">'.absint($_SESSION['wb_'.$date]).'</span> 个访问者</p>';
}
2. 在footer.php 或网页主页,合适的位置放入以下代码
<div><?php wb_echo_site_count(); ?></div>
THE END
二维码
打赏
文章目录
关闭
共有 0 条评论