Home > php教程 > php手册 > body text

wordpress公告栏数据库缓存版

WBOY
Release: 2016-06-06 20:08:34
Original
1583 people have browsed it

本来想大概八点二十发,但是没忍住. 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 .= '
Copy after login
  • '. 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 .= '
    Copy after login
  • '. 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', '');  
    }
    Copy after login

    改成

    function clear_buffer_function() {
    update_option('clear_buffer', ''); 
    update_option('clear_buffer2', '');
    //以此类推
    }
    Copy after login

    其他地方不动,即可

    福利

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

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!