WordPress免插件实现访问量统计!
捣鼓一下网站,WordPress免插件实现总访问量、日访问量、今天第几位访客等数据统计!
先在functions.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' => 999,//自定义初始访问数 '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:red">'.absint($sitecount['all']).'</span> 今日访问量:<span style="color:red">'.absint($sitecount['today']).'</span> 您是今天第:<span style="color:red">'.absint($_SESSION['wb_'.$date]).'</span> 个访问者</p>'; }
然后在需要调用的位置如footer.php中添加以下代码
<?php wb_echo_site_count(); ?>
如:<div style="text-align: center;background:#000;color:#FFF"> <?php wb_echo_site_count(); ?> </div>
空空如也!