兜兜转转,回归初恋,欢迎使用yusiyuhuo主题, 主题介绍>>>

免插件统计WordPress文章阅读数

WordPress建站 陌小雨 4942℃ 0评论

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

20160201115420

//文章点击数
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,统计出来的不是最真实的。

转载请注明:小雨科技 _武汉网站建设_武汉小程序搭建 » 免插件统计WordPress文章阅读数

喜欢 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址