您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

wordpress公告栏数据库缓存版

2024/6/22 3:23:19发布31次查看
本来想大概八点二十发,但是没忍住. wordpress公告栏数据库缓存版 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能 公告栏只展示管理员主评论,不展示子评论 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句 ?php bulletin
本来想大概八点二十发,但是没忍住.
wordpress公告栏数据库缓存版完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能公告栏只展示管理员主评论,不展示子评论以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句.
使用之前请把post_id' => 1884的1884改成你的公告栏页面idfunction 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 .= '
'. human_time_diff(get_comment_date('u',$comment->comment_id), current_time('timestamp')) . '前'.' ' . get_comment_date('h:i',$comment->comment_id) . '-comment_post_id)%20.%20 .>comment_id . ' title= '. strip_tags($comment->comment_content) . '>'. convert_smilies($comment->comment_content) . '';}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 .= '
'. human_time_diff(get_comment_date('u',$comment->comment_id), current_time('timestamp')) . '前'.' ' . get_comment_date('h:i',$comment->comment_id) . '-comment_post_id)%20.%20 .>comment_id . ' title= '. strip_tags($comment->comment_content) . '>'. convert_smilies($comment->comment_content) . '';}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', '');//以此类推}
其他地方不动,即可
福利今天的代码有点长有点复杂,为了奖励各位认真看完的小盆友,送上一组福利...
原文地址:wordpress公告栏数据库缓存版, 感谢原作者分享。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product