WP Super Cache 技巧:实时更新首页
2009年12月05日
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。
19 条评论 添加你的评论 →

1. Evlos | 2009:12:05 - 23:09:34 | #
光速过来把沙发座了 ~
感谢Twitter上的传送门 ~
2. QiuXianli | 2009:12:05 - 23:54:47 | #
还有板凳 呵呵
3. ehu4ever | 2009:12:06 - 07:54:24 | #
Askapache.com上说db-cache-reloaded的效果超过了wp-super-cache这咱页面缓存插件。这有有哪位试过了?另:http://wordpress.org/extend/plugins/db-cache-reloaded/。
4. yunshan | 2009:12:08 - 01:01:49 | #
我现在db-cache-reloaded和wp-super-cache一起用
5. 菠萝 | 2009:12:06 - 10:43:02 | #
代码控又开始发好东东啦!
6. keon | 2009:12:06 - 11:08:45 | #
很前很前~~
7. 水木鱼 | 2009:12:06 - 11:49:20 | #
第一次来,很喜欢你的博客
8. bolo | 2009:12:06 - 12:13:20 | #
先学习了,正准备着手优化博客的速度
9. 左岸 | 2009:12:06 - 14:34:29 | #
定时更新+回复更新
10. Apollools | 2009:12:06 - 15:11:12 | #
前十啊~
一直在用这个插件,实时刷新首页确实很有用!
11. flyinglife | 2009:12:06 - 19:30:52 | #
这个感觉不错啊,呵呵,先学习了,
很久没来留言了,
12. slashuse | 2009:12:07 - 00:38:57 | #
水煮鱼你好,
请问我在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删除后,还报此错?请问这是什么问题啊?
谢谢!
13. slashuse | 2009:12:07 - 00:42:35 | #
删除后,报错:
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
现在无法进入“登录页面”了…
14. Denis | 2009:12:07 - 00:42:37 | #
把 functions.php 格式改成 utf-8 without BOM
15. slashuse | 2009:12:07 - 01:06:29 | #
多谢Denis! 问题解决了!
16. slashuse | 2009:12:07 - 00:45:16 | #
to Denis ,
functions.php是UTF—8
17. AllenLu | 2009:12:07 - 19:59:34 | #
刚开始学习,可惜现在还看不懂
18. licream | 2009:12:19 - 07:18:14 | #
wp_cache_post_edit($front_page_id);
这里面的ID要不要用单引号?
wp_cache_post_edit(‘100′);
wp_cache_post_edit(100);
哪个是正确输出来的- -。我不懂PHP
19. Simple | 2009:12:23 - 13:30:43 | #
高手、、