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。

光速过来把沙发座了 ~
感谢Twitter上的传送门 ~
还有板凳 呵呵
Askapache.com上说db-cache-reloaded的效果超过了wp-super-cache这咱页面缓存插件。这有有哪位试过了?另:http://wordpress.org/extend/plugins/db-cache-reloaded/。
我现在db-cache-reloaded和wp-super-cache一起用
代码控又开始发好东东啦!
很前很前~~
第一次来,很喜欢你的博客
先学习了,正准备着手优化博客的速度
定时更新+回复更新
前十啊~
一直在用这个插件,实时刷新首页确实很有用!
这个感觉不错啊,呵呵,先学习了,
很久没来留言了,
水煮鱼你好,
请问我在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
现在无法进入“登录页面”了…
把 functions.php 格式改成 utf-8 without BOM
多谢Denis! 问题解决了!
to Denis ,
functions.php是UTF—8
刚开始学习,可惜现在还看不懂
wp_cache_post_edit($front_page_id);
这里面的ID要不要用单引号?
wp_cache_post_edit(’100′);
wp_cache_post_edit(100);
哪个是正确输出来的- -。我不懂PHP
高手、、