WordPress教程

不用任何插件实现WordPress文章点击数

阿里云

如果你是用 WordPress 搭建的网站,你是不是想知道你的文章被多少人点击看过?一些用于统计点击次数的插件可以解决这个问题,比如 PostViews,可是又怕插件会让网站变慢。今天要推荐的是一个更简单的不用任何插件实现点击数统计的方法。首先,需要创建相关的函数。你可以把下面的代码粘贴到你所用主题的 functions.php 文件里。

function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}

然后,粘贴下面的代码到主题的 single.php 的文章的 loop 里:

也想出现在这里?联系我们
创客主机
<?php setPostViews(get_the_ID()); ?>

最后,把下面的代码拷贝到任意你的主题模板里想要显示点击数的地方:

<?php echo getPostViews(get_the_ID()); ?>

不用任何插件实现 WordPress 文章点击数

已有 619 人购买
查看演示升级 VIP立刻购买

收藏
(1)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!