^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助,如果有代码出错,请联系站长解决
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 8年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 腾讯云3年2核2G新品轻量限时特惠只需408元

免插件统计WordPress文章阅读数

自己制作主题的话,就会需要这些小技巧了,现成的主题一般都自己集成。

免插件统计WordPress文章阅读数

2 核 2G 限时特惠 396 元/3 年    宝塔建站 10850 大礼包

//文章点击数
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()); ?>

代码弊端:刷新一次阅读数就+1,统计出来的不是最真实的。

赠人玫瑰,手有余香。