首页 > php教程 > php手册 > 正文

wordpress公告栏数据库缓存版

php中文网
发布: 2016-06-06 20:08:34
原创
1804人浏览过

本来想大概八点二十发,但是没忍住. wordpress公告栏数据库缓存版 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能 公告栏只展示管理员主评论,不展示子评论 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句 ?php bulletin

本来想大概八点二十发,但是没忍住.

wordpress公告栏数据库缓存版

  1. 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能
  2. 公告栏只展示管理员主评论,不展示子评论
  3. 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句.
    使用之前请把post_id' => 1884的1884改成你的公告栏页面id
function bulletin(){
if (!$announcement = get_option('clear_buffer')) {
$args = array('post_id' => 1884,'number' => 5,'user_id' => '1',parent => '0');$announcement = ''; $comments = get_comments($args);foreach ($comments as $comment) {$announcement .= '<li>'. human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')) . '前'.' ' . get_comment_date('H:i',$comment->comment_ID) . '-<a href="' . get_permalink($comment->comment_post_ID) . "#comment-" . $comment->comment_ID . '" title= "'. strip_tags($comment->comment_content) . '">'. convert_smilies($comment->comment_content) . '</a></li>';}update_option('clear_buffer', $announcement);
}
echo $announcement;
}
function clear_buffer_function() {
update_option('clear_buffer', ''); // 清空缓存 
}
add_action('comment_post', 'clear_buffer_function'); // 新评论发布,清空缓存
add_action('edit_comment', 'clear_buffer_function'); // 评论被编辑,清空缓存
add_action('trashed_comment', 'clear_buffer_function'); //评论移至回收站,清空缓存
登录后复制

原理.

通过update_option来在数据库中添加表以实现缓存,此代码具有通用性,如果想为其他函数实现缓存,只需参照这个例子来折腾.例如,我公告栏原代码为:

function bulletin(){
$args = array('post_id' => 1884,'number' => 5,'user_id' => '1',parent => '0');$announcement = ''; $comments = get_comments($args);foreach ($comments as $comment) {$announcement .= '<li>'. human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')) . '前'.' ' . get_comment_date('H:i',$comment->comment_ID) . '-<a href="' . get_permalink($comment->comment_post_ID) . "#comment-" . $comment->comment_ID . '" title= "'. strip_tags($comment->comment_content) . '">'. convert_smilies($comment->comment_content) . '</a></li>';}
echo $announcement;
}
登录后复制

可以对比文章开头那段代码,一起来找茬,找到的茬(也就是我用红色标注的地方)就是数据库缓存通用代码.
以此类推,还可以缓存更多函数,如果要给不同的函数缓存,只需把其他函数的if (!$announcement = get_option('clear_buffer'))改成if (!$announcement = get_option('clear_buffer2')) ,
然后把

function clear_buffer_function() {
update_option('clear_buffer', '');  
}
登录后复制

改成

function clear_buffer_function() {
update_option('clear_buffer', ''); 
update_option('clear_buffer2', '');
//以此类推
}
登录后复制

其他地方不动,即可

福利

今天的代码有点长有点复杂,为了奖励各位认真看完的小盆友,送上一组福利...
这不是特效,是镜子太脏了..

WPS零基础入门到精通全套教程!
WPS零基础入门到精通全套教程!

全网最新最细最实用WPS零基础入门到精通全套教程!带你真正掌握WPS办公! 内含Excel基础操作、函数设计、数据透视表等

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号