下面由/" target="_blank">wordpress教程栏目给大家介绍wordpress文章阅读量统计和显示(非插件, 刷新页面不累加),希望对需要的朋友有所帮助!
WordPress文章阅读量统计和显示(非插件, 刷新页面不累加)
WordPress文章阅读量统计实现思路:
每进入一次文章详情页面, 就会通过cookie判断该用户是否在设定的过期时间内访问过该文章, 若没有访问过, 则浏览次数增加一次。
实现流程如下:
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 文件, 时间间隔可自定义设置, 放在该文件最上面即可: