recherche
Maisonphp教程php手册wordpress公告栏数据库缓存版

wordpress公告栏数据库缓存版

Jun 06, 2016 pm 08:08 PM
wordpresstableau d'affichage数据库缓存

本来想大概八点二十发,但是没忍住. 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', '');
    //以此类推
    }

    其他地方不动,即可

    福利

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

    Déclaration
    Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

    Outils d'IA chauds

    Undresser.AI Undress

    Undresser.AI Undress

    Application basée sur l'IA pour créer des photos de nu réalistes

    AI Clothes Remover

    AI Clothes Remover

    Outil d'IA en ligne pour supprimer les vêtements des photos.

    Undress AI Tool

    Undress AI Tool

    Images de déshabillage gratuites

    Clothoff.io

    Clothoff.io

    Dissolvant de vêtements AI

    AI Hentai Generator

    AI Hentai Generator

    Générez AI Hentai gratuitement.

    Article chaud

    R.E.P.O. Crystals d'énergie expliqués et ce qu'ils font (cristal jaune)
    3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Meilleurs paramètres graphiques
    3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Comment réparer l'audio si vous n'entendez personne
    3 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: Comment déverrouiller tout dans Myrise
    4 Il y a quelques semainesBy尊渡假赌尊渡假赌尊渡假赌

    Outils chauds

    PhpStorm version Mac

    PhpStorm version Mac

    Le dernier (2018.2.1) outil de développement intégré PHP professionnel

    Envoyer Studio 13.0.1

    Envoyer Studio 13.0.1

    Puissant environnement de développement intégré PHP

    Adaptateur de serveur SAP NetWeaver pour Eclipse

    Adaptateur de serveur SAP NetWeaver pour Eclipse

    Intégrez Eclipse au serveur d'applications SAP NetWeaver.

    SublimeText3 version Mac

    SublimeText3 version Mac

    Logiciel d'édition de code au niveau de Dieu (SublimeText3)

    VSCode Windows 64 bits Télécharger

    VSCode Windows 64 bits Télécharger

    Un éditeur IDE gratuit et puissant lancé par Microsoft