当前位置:奉上博客 » 站长之家 » 正文

WordPress强化-给你的网站添加访问量统计代码

将下面的代码放到当前主题的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' => 0,
'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:#7df1ff">'.absint($sitecount['all']).'</span> &nbsp;&nbsp; 今日访问量:<span style="color:#7df1ff">'.absint($sitecount['today']).'</span> &nbsp;&nbsp; 您是今天第:<span style="color:#7df1ff">'.absint($_SESSION['wb_'.$date]).'</span> 位访问者</p>';

将下面的代码放到当前主题的footer.php文件中

<?php wb_echo_site_count(); ?>

相关文章