wordpress查詢瀏覽量需要用什么代碼

wordpress查詢瀏覽量需要用什么代碼

WordPress??搭建的博客或者企業站都有著同樣的需求,就是在內容頁面需要統計這個頁面的瀏覽次數,以方便站長查看某個頁面的仿問題,一般對代碼不熟悉的朋友都會用到瀏覽次數統計插件(wp-postviews),但是眾所周知,一個站點如果插件安裝的多了會對SEO優化??非常不利,那么下面給大家分享一個利用代碼來實現這種瀏覽次數的統計功能。

1、在functions.php函數文件的未尾另起一行,添加如下代碼。

<?php /* Postviews start */ function getPostViews($postID){     $count_key = &#39;post_views_count&#39;;     $count = get_post_meta($postID, $count_key, true);     if($count==&#39;&#39;){         delete_post_meta($postID, $count_key);         add_post_meta($postID, $count_key, &#39;0&#39;);         return " 0 ";     }     return $count; } function setPostViews($postID) {     $count_key = &#39;post_views_count&#39;;     $count = get_post_meta($postID, $count_key, true);     if($count==&#39;&#39;){         $count = 0;         delete_post_meta($postID, $count_key);         add_post_meta($postID, $count_key, &#39;0&#39;);     }else{        $count++;         update_post_meta($postID, $count_key, $count);     } } /* Postviews start end*/ ?>

2、在您需要顯示統計瀏覽次數的頁面,例如在single.php內容頁面中的循環語句的中間添加以下代碼:

<?php  setPostViews(get_the_ID());?>

添加后的顯示,如:

? ?????<?php  setPostViews(get_the_ID());?>

3、最后在您需要顯示統計的地方添加如下代碼:

<?php  echo getPostViews(get_the_ID()); ?>?次瀏覽

更多wordpress相關技術文章,請訪問wordpress教程欄目進行學習!

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享