搜索
首页php教程php手册wordpress公告栏数据库缓存版

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

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

wordpress公告栏数据库缓存版

  1. 完善了willin的数据库缓存代码,添加了删除评论时也刷新缓存的功能
  2. 公告栏只展示管理员主评论,不展示子评论
  3. 以下代码直接放在functions.php内,然后在需要展示公告栏的地方扔一句<?php bulletin();?>.
    使用之前请把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 .= '
  • '. human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')) . '前'.' ' . get_comment_date('H:i',$comment->comment_ID) . '-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_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', '');
    //以此类推
    }

    其他地方不动,即可

    福利

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

    声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    AI Hentai Generator

    AI Hentai Generator

    免费生成ai无尽的。

    热门文章

    R.E.P.O.能量晶体解释及其做什么(黄色晶体)
    4 周前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.最佳图形设置
    4 周前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.如果您听不到任何人,如何修复音频
    4 周前By尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O.聊天命令以及如何使用它们
    4 周前By尊渡假赌尊渡假赌尊渡假赌

    热工具

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    功能强大的PHP集成开发环境

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )专业的PHP集成开发工具

    Dreamweaver CS6

    Dreamweaver CS6

    视觉化网页开发工具

    VSCode Windows 64位 下载

    VSCode Windows 64位 下载

    微软推出的免费、功能强大的一款IDE编辑器

    Dreamweaver Mac版

    Dreamweaver Mac版

    视觉化网页开发工具