WordPress缓存文章浏览量访问不自动增加怎么办?WordPress缓存导致文章阅读数点赞数不更新

WordPress缓存导致文章阅读数点赞数不更新

WordPress 静态缓存目前对于何先生来说作用不是很大,比较适合一些流量比较大的博客或者网站。

之前接触过 WordPress 静态缓存,遇到过这个问题后面没有去深究。

WordPress缓存文章浏览量访问不自动增加修改正文

WordPress 开启静态缓存页面之后,当我们访问文章的时候,便不会去请求源站,而是返回缓存或者 CDN 页面,所以文章的浏览数量不会进行自增。

首先可以使用一个插件解决这种方法

答案是使用:安装WP-PostViews Plus插件

很容易吧,直接后台搜索WP-PostViews Plus,安装,然后启用就行了。

而且他兼容WP-PostViews插件,调用方式也一样:

在single.php你要显示统计的地方插入:

<?php if(function_exists('the_views')) { the_views(); } ?>

如果不使用插件实现可以使用下面的方法自己修改代码实现。

WordPress开启缓存后文章浏览量不增加解决办法

解决这个问题,我们可以通过 Ajax 来异步请求服务器,然后再进行文章的浏览数量自增或其他的操作。(注:本方法仅适用于使用了 WP-PostViews 插件或者和具有 WP-PostViews 相同字段的博客,若你的文章统计字段不同,可自行修改下方 SQL 语句以达到效果)

首先,我们在网站的根目录新建一个操作文件,命名为views.php,将以下代码写入此文件中。

<?php 
require('../../wp-blog-header.php');
$isXhr = $_SERVER['HTTP_X_REQUESTED_WITH'];
if (strtoupper($isXhr) == strtoupper("XMLHttpRequest")) {
   $post_id = $_POST['post_id'];
   $po = get_post_meta($post_id,'views',true);
   if(!$po)  {
       //表示不存在这个自定义栏目,那么新增这个自定义栏目
       add_post_meta($post_id, 'views', 1, true);
       echo 1;
   } else {
       // 若已存在这个自定义栏目,则先获取当前数值,并递增+1,再更新数据库
       $count = $po + 1;
       update_post_meta($post_id,'views',$count);
       echo $count;
   }
} else {
   header("Status: 403 Not Found");
   exit;
}

在上述代码中,为了防止恶意请求,做了一个 Ajax 请求头的验证,验证是否为 xmlHttpRequest 请求,如果不是则返回 403 状态。

接着我们在主题的 page.php 文件中插入以下代码:

<script>
$(function(){
   function page_views() {
       if(!window.localStorage) {
           console.info('浏览器不支持 localStorage');
       }else{
           var post_id = <?php the_ID();?>;
           var views = localStorage.getItem("view_list");
           if(views===null){
               localStorage.setItem("view_list",post_id);
               $.post("/views.php?time="+new Date().getTime(),{post_id:post_id});
               return true;
           }else{
               var item = views.split("|");
               var bool = $.inArray(post_id+'',item);
               if(bool===-1){
                   $.post("/views.php?time="+new Date().getTime(),{post_id:post_id});
                   localStorage.setItem("view_list",views+"|"+post_id);
                   return true;
               }
               return false;
           }
       }
   }
   page_views();
});
</script>

完成。

官方QQ群号码:922069959

您可能感兴趣的文章

留言评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select * from vps_autoblog_queue where is_running >0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=6 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=6 and source_url_key='85f8e27ce55bd78fd33f47d2b1409198'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(6,'https://www.hijk.pw/post-sitemap.xml','85f8e27ce55bd78fd33f47d2b1409198',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=4 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=4 and source_url_key='54b1dccf98c13c81f546faf82132bc77'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(4,'https://ssr.tools/sitemap-posttype-post.2020.xml','54b1dccf98c13c81f546faf82132bc77',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=2 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=2 and source_url_key='91372ddcd9e4809b115feaa5b6f00815'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(2,'https://tlanyan.me/post-sitemap.xml','91372ddcd9e4809b115feaa5b6f00815',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=7 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=7 and source_url_key='569e17863f7693f1afa9e9256af9dcff'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(7,'https://aisoa.cn/sitemap.xml','569e17863f7693f1afa9e9256af9dcff',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=9 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=9 and source_url_key='cc9416f8df40d8c3a48e66856b1cd6fd'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(9,'https://www.v2rayssr.com/sitemap.xml','cc9416f8df40d8c3a48e66856b1cd6fd',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select count(*) from vps_autoblog_queue where task_id=10 and is_running>0

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,fetched,is_running,last_date_time from vps_autoblog_queue where task_id=10 and source_url_key='74714f67855820f0c701ce905b231f27'

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
insert into vps_autoblog_queue(task_id,source_url,source_url_key,create_date_time,not_check_stoped,post_interval) values(10,'https://doubibackup.com/sitemap.xml','74714f67855820f0c701ce905b231f27',1750370672,0,0)

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,is_running,last_post_time,post_interval,not_check_stoped from vps_autoblog_queue WHERE is_running>0 and fetched=0 order BY id ASC

WordPress数据库错误: [Table 'ssrvps.vps_autoblog_queue' doesn't exist]
select id,sn_id from vps_autoblog_queue WHERE is_running=0 and fetched=0 order BY id ASC LIMIT 1