标签导航:

下面由wordpress/" target="_blank">wordpress教程栏目给大家介绍wordpress文章阅读量统计和显示(非插件, 刷新页面不累加),希望对需要的朋友有所帮助!

WordPress文章阅读量统计实现思路:

实现流程如下:

1.添加以下代码至主题的functions.php文件, 放在该文件最下面即可:

function getPostViews($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count=='' || !$count){
        return "0";
    }
    return $count;
}
function setPostViews($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
    if($count=='' || !$count) {
        $count = 1;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, $count);
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

2.添加以下代码至主题的single.php 文件, 时间间隔可自定义设置, 放在该文件最上面即可:

<?php  
if(!isset($_COOKIE[&#39;views&#39;.$post->ID.COOKIEHASH]) || $_COOKIE['views'.$post-&gt;ID.COOKIEHASH] != '1'){
    setPostViews($post-&gt;ID);
    setcookie('views'.$post-&gt;ID.COOKIEHASH,'1',time() + 99999999,COOKIEPATH,COOKIE_DOMAIN);
} 
?&gt;
<?php  echo getPostViews(get_the_ID());?>

以下是我的个人博客  添加展示阅读量的代码和实际显示效果。

5e1e6867e8e1fcabd233852c4574f83.png