本教程是為了解決那些剛剛創建的網站,被訪客瀏覽時,網站文章閱讀量數字顯示個位或是十位數的尷尬,以下內容只針對于主題安裝或是集成了wp-postviews插件的wordpress主題,通過以下wordpress插件的修改方法與wordpress主題的修改方法實現流量爆棚的效果。經測試,完美兼容dux主題。
后臺模板函數 (functions.php),在最后添加如下代碼即可,其中范圍數值可根據實際情況自行修改:
/* 文章瀏覽數量統計
/* ——————————– */
add_action( ‘wp_footer’, ‘cx_statistics_visitors’,9999 );
/* 文章瀏覽數量統計
/* ——————————– */
function cx_statistics_visitors( $cache = false ){
global $post;
//頁面判斷
if(is_single()):
//非緩存模式獲取id
$id = $post->ID;
//判斷條件,不滿足條件終止執行
if( ( !is_singular() && !$cache ) || !$id ) return false;
if( WP_CACHE && !$cache ){?>
<script type=”text/javascript”>
$(function(){
$.ajax({
type: ‘GET’,
dataType: ‘html’,
url: chenxing.ajax_url,
data:”id=<?php echo $id;?>&action=visitors”,
cache: false,
success: function(data){
if(data>0){
$(‘.cx-views’).text(data/10);
}
}
}
);
});
</script>
<?php
return false;
}else{
$post_views = (get_post_meta($id,’views’,true))?(int) get_post_meta($id,’views’,true ):0;
update_post_meta($id,’views’,($post_views +mt_rand(61, 367)));
}
endif;
}
function Bing_statistics_cache(){
//緩存模式獲取文章id
$id = ($_GET[‘id’])?$_GET[‘id’]:0;
if($id){
$post_views = (get_post_meta($id,’views’,true))?(int) get_post_meta($id,’views’,true ):0;
update_post_meta($id,’views’,($post_views +mt_rand(56, 267)));
echo $post_views+1;
}else{
echo ‘-1’;
}
}
add_action( ‘wp_ajax_nopriv_visitors’, ‘Bing_statistics_cache’ );
add_action( ‘wp_ajax_visitors’, ‘Bing_statistics_cache’ );
function Bing_get_views($display = true ,$id = 0){
global $post;
if($id == 0){
$post_id = $post->ID;
}else{
$post_id = $id;
}
$views = (int) get_post_meta( $post_id, ‘views’, true );
if($display) {
if($views>1000000){
echo ‘100萬+</br>’;
}else if($views>10000){
echo round(($views/10000),1).’ 萬’;
}else if($views>1000){
echo $views;
}else{
echo $views;
}
} else {
return $views;
}
}



















