我爱水煮鱼 » WordPress » WP Super Cache 技巧:实时更新首页

WP Super Cache 技巧:实时更新首页

WP Super Cache 是我推荐的最佳 WordPress 缓存插件,它把整个页面直接生成 HTML 文件,这样 Apache 就不用解析 PHP 脚本,通过使用这个插件,能使得你的 WordPress 博客将显著的提速。

如果 WordPress 的首页显示的不是最新日志,而是一个页面,并且这个页面包含实时更新的部分,如我爱水煮鱼的首页包含最新日志,那么就会存在一个问题,那么这个页面就不会实时更新。

这里我提供一个技巧,可以让你在使用 WP Super Cache 的这个静态缓存插件提速的同时,也能够让首页实时更新,WP Super Cache 是通过 wp_cache_post_edit 这个函数来编辑缓存的,它的参数是日志或者页面的 ID。

所以我们首先获取用于显示首页的页面的 ID,然后在日志更新或者删除的时候,或者有新留言的时候(如果首页没有包含最新留言,这个可不必),刷新首页。所以大致的代码如下:

<?php
add_action('publish_post', 'refresh_front_page', 0); //发布或者更新日志时候刷新首页
add_action('edit_post', 'refresh_front_page', 0); //有新留言或者留言被删除的时候刷新首页
add_action('delete_post', 'refresh_front_page', 0); //删除日志时候刷新首页
add_action('publish_phone', 'refresh_front_page', 0); //通过 email 发布日志之后刷新首页
 
function refresh_front_page(){
    $front_page_id = get_option('page_on_front'); //获取显示首页的页面 ID
    wp_cache_post_edit($front_page_id); //刷新该页
}
?>

把这段代码复制到主题的 functions.php 即可。

当然如果你想刷新某个页面,也可以是用 wp_cache_post_edit 这个刷新它,参数是页面的 ID。

标签:

分享到:

请选择你看完该文章的感受:

不错 超赞 无聊 扯谈 不解 路过
  1. Askapache.com上说db-cache-reloaded的效果超过了wp-super-cache这咱页面缓存插件。这有有哪位试过了?另:http://wordpress.org/extend/plugins/db-cache-reloaded/。

  2. 水煮鱼你好,
    请问我在functions.php 中加入上述代码后,为什么在“登录页面”/wp-admin 中提示错误如下:
    Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-includes/pluggable.php on line 865

    把上述代码从functions.php删除后,还报此错?请问这是什么问题啊? :?:
    谢谢!

    • 删除后,报错:
      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-login.php on line 290

      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-login.php on line 302

      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-includes/pluggable.php on line 662

      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-includes/pluggable.php on line 663

      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-includes/pluggable.php on line 664

      Warning: Cannot modify header information – headers already sent by (output started at /home2/dbtancom/public_html/wp-content/themes/webby-green-10/functions.php:1) in /home2/dbtancom/public_html/wp-includes/pluggable.php on line 865

      现在无法进入“登录页面”了… :sad:

  3. wp_cache_post_edit($front_page_id);
    这里面的ID要不要用单引号?
    wp_cache_post_edit(’100′);
    wp_cache_post_edit(100);
    哪个是正确输出来的- -。我不懂PHP :shock: