博客各种自定义添加存档

Posted on Posted in wordpress

theme 中的 functions.php 文件

  1. 英文转中文的问题
remove_filter('the_content', 'wptexturize');
  1. allpost
 function zww_archives_list() {
     if( !$output = get_option('zww_archives_list') ){
         $output = '<div id="archives"><p>[<a id="al_expand_collapse" href="#">全部展开/收缩</a>] <em>(注: 点击月份可以展开)</em></p>';
         $the_query = new WP_Query( 'posts_per_page=-1&ignore_sticky_posts=1' ); //update: 加上忽略置顶文章
         $year=0; $mon=0; $i=0; $j=0;
         while ( $the_query->have_posts() ) : $the_query->the_post();
             $year_tmp = get_the_time('Y');
             $mon_tmp = get_the_time('m');
             $y=$year; $m=$mon;
             if ($mon != $mon_tmp && $mon > 0) $output .= '</ul></li>';
             if ($year != $year_tmp && $year > 0) $output .= '</ul>';
             if ($year != $year_tmp) {
                 $year = $year_tmp;
                 $output .= '<h3 class="al_year">'. $year .' 年</h3><ul class="al_mon_list">'; //输出年份
             }
             if ($mon != $mon_tmp) {
                 $mon = $mon_tmp;
                 $output .= '<li><span class="al_mon">'. $mon .' 月</span><ul class="al_post_list">'; //输出月份
             }
             $output .= '<li>'. get_the_time('d日: ') .'<a href="'. get_permalink() .'">'. get_the_title() .'</a> <em>('. get_comments_number('0', '1', '%') .')</em></li>'; //输出文章日期和标题
         endwhile;
         wp_reset_postdata();
         $output .= '</ul></li></ul></div>';
         update_option('zww_archives_list', $output);
     }
     echo $output;
 }
 function clear_zal_cache() {
     update_option('zww_archives_list', ''); // 清空 zww_archives_list
 }
 add_action('save_post', 'clear_zal_cache'); // 新发表文章/修改文章时
  1. 添加老文章提示信息
function old_content_message($content) {
    $modified = get_post_time('U');
    $current = current_time('timestamp');
    $diffTime = ($current - $modified) / (60 * 60 * 24);
     if ($diffTime > 730) {
    $content = '<div class="warn">Tips: 本文创建于'.get_post_time('Y年n月j日').
     ',已超过 2 年,内容或图片可能已经失效!</div>'.$content;
     }
     return $content;
}
add_filter('the_content', 'old_content_message');
  1. WordPress支持中文用户名
function fanly_sanitize_user ($username, $raw_username, $strict) {
    $username = wp_strip_all_tags( $raw_username );
    $username = remove_accents( $username );
    $username = preg_replace( '|%([a-fA-F0-9][a-fA-F0-9])|', '', $username );
    $username = preg_replace( '/&.+?;/', '', $username );
    if ($strict) {
        $username = preg_replace ('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
    }
    $username = trim( $username );
    $username = preg_replace( '|\s+|', ' ', $username );
    return $username;
}
add_filter ('sanitize_user', 'fanly_sanitize_user', 10, 3);

alltags.php

<div>
<?php
$all_the_tags = get_tags();
shuffle($all_the_tags);
foreach($all_the_tags as $this_tag): ?>
<?php   
    $tag_name = $this_tag->name;
    $loc = get_tag_link($this_tag->term_id); 
?>
<span style="margin-top:<?php echo rand(0,20);?>px;margin-bottom:<?php echo rand(0,20);?>px;margin-left:<?php echo rand(0,20);?>px;margin-right:<?php echo rand(0,20);?>px;">
<a style="<?php $color = rand(0,255);
echo 'color:rgb(',rand(0,255).','.rand(0,255).','.rand(0,255).');font-size:'.rand(1,25).'px';
?>" href="<?php echo $loc; ?>">
<?php echo $tag_name; ?></a>    
</span>
<?php endforeach; ?>
</div>

allpost.php

<script>
jQuery(document).ready(function($){
 //===================================存档页面 jQ伸缩
     (function(){
         $('#al_expand_collapse,#archives span.al_mon').css({cursor:"s-resize"});
         $('#archives span.al_mon').each(function(){
             var num=$(this).next().children('li').size();
             var text=$(this).text();
             $(this).html(text+'<em> ( '+num+' 篇文章 )</em>');
         });
         var $al_post_list=$('#archives ul.al_post_list'),
             $al_post_list_f=$('#archives ul.al_post_list:first');
         $al_post_list.hide(1,function(){
             $al_post_list_f.show();
         });
         $('#archives span.al_mon').click(function(){
             $(this).next().slideToggle(400);
             return false;
         });
         $('#al_expand_collapse').toggle(function(){
             $al_post_list.show();
         },function(){
             $al_post_list.hide();
         });
     })();
 });
</script>

header.php

  1. 增加favicon
<link rel="apple-touch-icon" sizes="57x57" href="/favicon/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="/favicon/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="/favicon/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="/favicon/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="/favicon/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="/favicon/apple-touch-icon-120x120.png">
<link rel="icon" type="image/png" href="/favicon/favicon-32x32.png" sizes="32x32">
<link rel="icon" type="image/png" href="/favicon/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicon/favicon-16x16.png" sizes="16x16">
<link rel="manifest" href="/favicon/manifest.json">
<link rel="mask-icon" href="/favicon/safari-pinned-tab.svg" color="#5bbad5">
<link rel="shortcut icon" href="/favicon/favicon.ico">
<meta name="msapplication-TileColor" content="#ffc40d">
<meta name="msapplication-TileImage" content="/favicons/mstile-144x144.png">
<meta name="msapplication-config" content="/favicons/browserconfig.xml">
<meta name="theme-color" content="#ffffff">
<meta name="robots" content="noarchive">
<link rel="alternate" hreflang="zh-Hans" href="http://liuhonghe.me">
<script src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
  1. 增加高亮代码
<link rel="stylesheet" href="http://qiniu.liuhonghe.me/wp-content/themes/iconic-one/highlight/styles/solarized-dark.css">
<script src="http://qiniu.liuhonghe.me/wp-content/themes/iconic-one/highlight/highlight.pack.js"></script>
<script>
hljs.initHighlightingOnLoad();
</script>
<style>
.hljs-line-numbers {
    text-align: right;
    border-right: 1px solid #ccc;
    color: #999;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
pre {
    position: relative;
    margin-bottom: 24px;
    border-radius: 3px;
    border: 1px solid #C3CCD0;
    background: #FFF;
    overflow: hidden;
    white-space: pre-wrap;
    word-wrap: break-word;
}
</style>
  1. 增加打字效果
<script src="//cdn.bootcss.com/typed.js/1.1.6/typed.min.js"></script>
<?php
        $cibaUrl = 'http://open.iciba.com/dsapi/';
        function getJsonToObj($url){
            $getJson = file_get_contents($url);
            $getObj = json_decode($getJson);
            return $getObj;
        }
        $yuju = getJsonToObj($cibaUrl);
// echo $yuju->content . $yuju->note;
        ?>
<script>
$(function(){
      $(".containerwords").typed({
        strings: ["<?php echo $yuju->content .'<br />'. $yuju->note; ?>"],
        contentType: 'html',
        showCursor: false,
        typeSpeed: 70
      });
  });
</script> 
  1. 增加桌面通知,全屏显示
<script>
            function showNotice(title,msg){
                var Notification = window.Notification || window.mozNotification || window.webkitNotification;
                if(Notification){
                    Notification.requestPermission(function(status){
                        //status默认值'default'等同于拒绝 'denied' 意味着用户不想要通知 'granted' 意味着用户同意启用通知
                        if("granted" != status){
                            return;
                        }else{
                            var tag = "sds"+Math.random();
                            var notify = new Notification(
                                title,
                                {
                                    dir:'auto',
                                    lang:'zh-CN',
                                    tag:tag,//实例化的notification的id
                                    icon:'http://liuhonghe.me/favicon/yueliang.png',//通知的缩略图,//icon 支持ico、png、jpg、jpeg格式
                                    body:msg //通知的具体内容
                                }
                            );
                            notify.onclick=function(){
                                //如果通知消息被点击,通知窗口将被激活
                                window.focus();
                            },
                            notify.onerror = function () {
                                console.log("HTML5桌面消息出错!!!");
                            };
                            notify.onshow = function () {
                                setTimeout(function(){
                                    notify.close();
                                },1000 * 60)
                            };
                            notify.onclose = function () {
                                console.log("HTML5桌面消息关闭!!!");
                            };
                        }
                    });
                }else{
                    console.log("您的浏览器不支持桌面消息");
                }
            };
            showNotice("","What are U 弄啥嘞?")
            
            
            // Find the right method, call on correct element
function launchFullscreen(element) {
  if(element.requestFullscreen) {
    element.requestFullscreen();
  } else if(element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if(element.webkitRequestFullscreen) {
    element.webkitRequestFullscreen();
  } else if(element.msRequestFullscreen) {
    element.msRequestFullscreen();
  }
}

function exitFullscreen() {
  if(document.exitFullscreen) {
    document.exitFullscreen();
  } else if(document.mozCancelFullScreen) {
    document.mozCancelFullScreen();
  } else if(document.webkitExitFullscreen) {
    document.webkitExitFullscreen();
  }
}

function dumpFullscreen() {
  console.log("document.fullscreenElement is: ", document.fullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement);
  console.log("document.fullscreenEnabled is: ", document.fullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled);
}

// Events
document.addEventListener("fullscreenchange", function(e) {
  console.log("fullscreenchange event! ", e);
});
document.addEventListener("mozfullscreenchange", function(e) {
  console.log("mozfullscreenchange event! ", e);
});
document.addEventListener("webkitfullscreenchange", function(e) {
  console.log("webkitfullscreenchange event! ", e);
});
document.addEventListener("msfullscreenchange", function(e) {
  console.log("msfullscreenchange event! ", e);
});

// Add different events for fullscreen
        </script>

// 在合适的位置添加
<span>
    <button style="padding: 2px;" onclick="launchFullscreen(document.documentElement);" class="sexyButton">进入全屏</button>
    <button style="padding: 2px;"onclick="exitFullscreen();" class="sexyButton">退出全屏</button>
</span>

footer.php

  1. 球统计
<script type="text/javascript" src="//ra.revolvermaps.com/0/0/3.js?i=0b7jckrlhyx&amp;b=0&amp;s=20&amp;m=2&amp;cl=007eff&amp;co=010020&amp;cd=aa0000&amp;v0=60&amp;v1=60&amp;r=1" async="async"></script>
  1. 各种统计mate
<!--百度统计-->
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "//hm.baidu.com/hm.js?3ef8575e9b9115f919efb86c408ae05a";
  var s = document.getElementsByTagName("script")[0];
  s.parentNode.insertBefore(hm, s);
})();
</script>
<!--百度统计 end-->
<!--百度自动推送-->
<script>
(function(){
    var bp = document.createElement('script');
    bp.src = '//push.zhanzhang.baidu.com/push.js';
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
<!--end 百度自动推送-->
<!--百度分享
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"slide":{"type":"slide","bdImg":"0","bdPos":"right","bdTop":"64"},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
end 百度分享-->
<!--360自动推送-->
<script>
(function(){
   var src = (document.location.protocol == "http:") ? "http://js.passport.qihucdn.com/11.0.1.js?ab1558a891f661fca84df4706dd6cb3e":"https://jspassport.ssl.qhimg.com/11.0.1.js?ab1558a891f661fca84df4706dd6cb3e";
   document.write('<script src="' + src + '" id="sozz"><\/script>');
})();
</script>
<!--end 360自动推送-->
<!--谷歌分析-->
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-45575917-1', 'auto');
  ga('send', 'pageview');
</script>
<!--谷歌分析 end-->
<!--心知 start-->
<script>(function(T,h,i,n,k,P,a,g,e){g=function(){P=h.createElement(i);a=h.getElementsByTagName(i)[0];P.src=k;P.charset="utf-8";P.async=1;a.parentNode.insertBefore(P,a)};T["ThinkPageWeatherWidgetObject"]=n;T[n]||(T[n]=function(){(T[n].q=T[n].q||[]).push(arguments)});T[n].l=+new Date();if(T.attachEvent){T.attachEvent("onload",g)}else{T.addEventListener("load",g,false)}}(window,document,"script","tpwidget","//widget.thinkpage.cn/widget/chameleon.js"))</script>
<script>tpwidget("init", {
    "flavor": "bubble",
    "location": "WX4FBXXFKE4F",
    "geolocation": "enabled",
    "position": "bottom-left",
    "margin": "0px 0px",
    "language": "auto",
    "unit": "c",
    "theme": "chameleon",
    "uid": "U755454303",
    "hash": "4ded8bea85babf2def4a46e82d525b55"
});
tpwidget("show");</script>
<!--心知 end-->
» 转载请注明来源:若我若鱼 » 博客各种自定义添加存档

Leave a Reply

Your email address will not be published. Required fields are marked *

7 + 20 =