今天无意间发现评论换个浏览器就看不见一想就是缓存的问题,但是我使用的是《WordPress安装memcached提升网站速度》没有设置项无法将评论排除在外。
方法一
编辑主题下的 comments-ajax.php 文件(启用 ajax 评论的博客才有),找到如下代码:
do_action('pre_comment_on_post', $comment_post_ID);
然后,在这行代码之后添加删除缓存代码,保存即可:
- //有人评论将自动删除已存在缓存
- $post_data = get_post($post->ID, ARRAY_A);
- $slug = $post_data['post_name'];
- $cache_s = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$post->ID.".html/index.html";
- $cache_sd = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$post->ID.".html";
- $cache_p = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$slug."/index.html";
- $cache_pd = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$slug;
- if (file_exists($cache_s)) {
- if (unlink($cache_s) === false) {
- error_log("Failed to delete cache file: $cache_s");
- }
- if (rmdir($cache_sd) === false) {
- error_log("Failed to remove directory: $cache_sd");
- }
- }
方法二
将以下代码添加到主题functions.php文件中
- add_action('wp_insert_comment', function($comment_id) {
- $comment = get_comment($comment_id);
- $post_id = $comment->comment_post_ID;
- // 清理与文章相关的缓存
- $slug = get_post_field('post_name', $post_id);
- $cache_s = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$post_id.".html/index.html";
- $cache_sd = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$post_id.".html";
- $cache_p = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$slug."/index.html";
- $cache_pd = WP_CONTENT_DIR."/cache/supercache/".$_SERVER['SERVER_NAME']."/".$slug;
- if (file_exists($cache_s)) {
- unlink($cache_s);
- rmdir($cache_sd);
- }
- if (file_exists($cache_p)) {
- unlink($cache_p);
- rmdir($cache_pd);
- }
- }, 10, 1);
2024年10月19日 上午12:28 沙发
发不了任何评论,你这
2024年10月19日 上午1:29 1层
@老白博客 发布后没有显示评论吗?