<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>雨天阳光 &#187; 归档</title>
	<atom:link href="http://shit.name/tag/%e5%bd%92%e6%a1%a3/feed/" rel="self" type="application/rss+xml" />
	<link>http://shit.name</link>
	<description>此人之水镜，见之莹然，若披云雾而睹青天也。</description>
	<lastBuildDate>Wed, 31 Dec 2014 06:21:04 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=4.0.33</generator>
	<item>
		<title>利用WP原生函数纯代码实现WordPress归档页面模板</title>
		<link>http://shit.name/pure-code-of-archives/</link>
		<comments>http://shit.name/pure-code-of-archives/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 10:24:26 +0000</pubDate>
		<dc:creator><![CDATA[Oo雨天阳光oO]]></dc:creator>
				<category><![CDATA[互联二亩田]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[原生函数]]></category>
		<category><![CDATA[归档]]></category>
		<category><![CDATA[纯代码]]></category>

		<guid isPermaLink="false">http://shit.name/?p=174</guid>
		<description><![CDATA[比起其他的收费主题，Mossight唯一的不足就是没有现成的归档页面，个人觉得归档页面对于一个博客来说还是比较 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><img src="http://shit.name/wp-content/uploads/2012/08/archives.jpg" alt="" title="archives" width="614" height="140" class="alignnone size-full wp-image-175" /><br />
比起其他的收费主题，<a title="Mossight收费主题" href="http://mossight.jezzzz.me/" target="_blank">Mossight</a>唯一的不足就是没有现成的归档页面，个人觉得归档页面对于一个博客来说还是比较重要，所以自己动手，丰衣足食。</p>
<p>对于这个问题最简单的办法就是弄个归档页面的插件。不过插件让WordPress越发臃肿，所以秉着简洁高效的原则，我还是想通过代码来实现这个功能，通过"谷哥"和"度娘"，半个码农的我也是能解决这个问题的。</p>
<p>网上看下来，关于用代码写归档页大致有两种，一是直接使用 SQL 语句实现调用，另外一种是使用WordPress 推荐的 WP_Query() 函数来调用。WordPress 官方不提倡直接使用 SQL 语句实现，所以我会参照后面一种方法进行。</p>
<p>下面方法每个步骤都分两种，一种是对于一般主题，另外一种是针对<a title="Mossight收费主题" href="http://mossight.jezzzz.me/">Mossight</a>主题的。如各位看客需了解其原理，请移步<strong>原创作者：</strong><a title="WordPress归档页面" href="http://zww.me/archives/25589" target="_blank">ZWWoOoOo</a>（毕竟借鉴他人的没有那么足底气跟人讲解 呵呵）查看具体效果：请看<a title="归档" href="http://shit.name/archives" target="_blank">我博客的存档页</a><span id="more-174"></span></p>
<p>1、原教程说把下面的函数扔到所用主题的 functions.php 文件（ 为了避免中文乱码，将其转化为UTF8 无 BOM 格式）里面：</p>
<pre class="brush: php; title: ; notranslate">/* Archives list by zwwooooo | http://zww.me */
 function zww_archives_list() {
     if( !$output = get_option('zww_archives_list') ){
         $output = '&lt;div id=&quot;archives&quot;&gt;&lt;p&gt;[&lt;a id=&quot;al_expand_collapse&quot; href=&quot;#&quot;&gt;全部展开/收缩&lt;/a&gt;] &lt;em&gt;(注: 点击月份可以展开)&lt;/em&gt;&lt;/p&gt;';
         $the_query = new WP_Query( 'posts_per_page=-1&amp;ignore_sticky_posts=1' ); //update: 加上忽略置顶文章
         $year=0; $mon=0; $i=0; $j=0;
         while ( $the_query-&gt;have_posts() ) : $the_query-&gt;the_post();
             $year_tmp = get_the_time('Y');
             $mon_tmp = get_the_time('m');
             $y=$year; $m=$mon;
             if ($mon != $mon_tmp &amp;&amp; $mon &gt; 0) $output .= '&lt;/ul&gt;&lt;/li&gt;';
             if ($year != $year_tmp &amp;&amp; $year &gt; 0) $output .= '&lt;/ul&gt;';
             if ($year != $year_tmp) {
                 $year = $year_tmp;
                 $output .= '&lt;h3 class=&quot;al_year&quot;&gt;'. $year .' 年&lt;/h3&gt;&lt;ul class=&quot;al_mon_list&quot;&gt;'; //输出年份
             }
             if ($mon != $mon_tmp) {
                 $mon = $mon_tmp;
                 $output .= '&lt;li&gt;&lt;span class=&quot;al_mon&quot;&gt;'. $mon .' 月&lt;/span&gt;&lt;ul class=&quot;al_post_list&quot;&gt;'; //输出月份
             }
             $output .= '&lt;li&gt;'. get_the_time('d日: ') .'&lt;a href=&quot;'. get_permalink() .'&quot;&gt;'. get_the_title() .'&lt;/a&gt; &lt;em&gt;('. get_comments_number('0', '1', '%') .')&lt;/em&gt;&lt;/li&gt;'; //输出文章日期和标题
         endwhile;
         wp_reset_postdata();
         $output .= '&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;';
         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'); // 新发表文章/修改文章时</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>本人启用了Mossight-child子主题，而且这个子主题里的functions.php 文件内容为空，所以直接将上面代码复制到functions.php 文件内即可</div></div>
<p>2. 复制一份主题的 page.php 更名为 archives.php，然后在最顶端加入：</p>
<pre class="brush: php; title: ; notranslate">&lt;?php
/* Template Name: archives */
?&gt;</pre>
<p>然后找到其中类似</p>
<pre class="brush: php; title: ; notranslate">&lt;?php content(); ?&gt;</pre>
<p>代码，在其下面加入如下代码：</p>
<pre class="brush: php; title: ; notranslate">&lt;!--archives--&gt;
&lt;?php zww_archives_list(); ?&gt;
&lt;!--archives end--&gt;</pre>
<p>然后进wp后台添加一新页面，在右侧栏模板选择 archives。<br />
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>将Mossight文件夹里的page.php按照上面的方法更改即可。</div></div></p>
<p>3. 给主题加载 jQuery 库，没有加载的，把下面这句扔到 functions.php 里面就行了。</p>
<pre class="brush: php; title: ; notranslate">wp_enqueue_script('jquery');</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>由于我已启用主题调用的新浪jquery库，所以这步省略。</div></div>
<p>4. jQuery 效果代码</p>
<pre class="brush: jscript; title: ; notranslate">jQuery(document).ready(function($){
 //===================================存档页面 jQ伸缩
     (function(){
         $('#al_expand_collapse,#archives span.al_mon').css({cursor:&quot;s-resize&quot;});
         $('#archives span.al_mon').each(function(){
             var num=$(this).next().children('li').size();
             var text=$(this).text();
             $(this).html(text+'&lt;em&gt; ( '+num+' 篇文章 )&lt;/em&gt;');
         });
         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();
         });
     })();
 });</pre>
<p>PS：不知道怎么写js文件调用的就直接打开 header.php 并找到
<pre class="brush: php; title: ; notranslate">&lt;?php wp_head(); ?&gt;</pre>
<p>在其下面加上</p>
<pre class="brush: jscript; title: ; notranslate">&lt;script type=&quot;text/javascript&quot;&gt;上面那段jQuery代码&lt;/script&gt;</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>个人认为上面那段jQuery效果代码应该只作用于归档页面，如果贸然添加到header.php这全局文件可能会影响到别页面的其他jQuery效果，所以我将上面那段代码添加到之前创建的archives.php文件里，找到
<pre class="brush: php; title: ; notranslate">&lt;?php get_header()?&gt;</pre>
<p>并在下面添加</p>
<pre class="brush: jscript; title: ; notranslate">&lt;script type=&quot;text/javascript&quot;&gt;上面那段jQuery代码&lt;/script&gt;</pre>
<p>即可。</div></div>
<blockquote><p><span style="color: #993300;">到这步，归档页面的功能已经都实现了，如果对其原始的样式不满意，还可以根据下面给出的HTML结构进行美化。</span></p></blockquote>
<p>5.css根据需要写，不写也可以。HTML结构如下：
<pre class="brush: php; title: ; notranslate">&lt;div id=&quot;archives&quot;&gt;
     &lt;p&gt;[&lt;a id=&quot;al_expand_collapse&quot; href=&quot;#&quot;&gt;全部展开/收缩&lt;/a&gt;] &lt;em&gt;(注: 点击月份可以展开)&lt;/em&gt;&lt;/p&gt;
     &lt;h3 class=&quot;al_year&quot;&gt;'年份&lt;/h3&gt;
     &lt;ul class=&quot;al_mon_list&quot;&gt;
         &lt;li&gt;&lt;span class=&quot;al_mon&quot;&gt;月份&lt;em&gt; (本月文章数量)&lt;/em&gt;&lt;/span&gt;
             &lt;ul class=&quot;al_post_list&quot;&gt;
                 &lt;li&gt;号数: &lt;a href=&quot;文章链接&quot;&gt;文章标题&lt;/a&gt; &lt;em&gt;(评论数量)&lt;/em&gt;&lt;/li&gt;
             &lt;/ul&gt;
         &lt;/li&gt;
     &lt;/ul&gt;
 &lt;/div</pre>
<div class='newcode'><h2>Mossight主题</h2><div class='newcontent'>我这里只做了相关段落的缩进。<br />
同样在之前创建的archives.php文件里，找到
<pre class="brush: php; title: ; notranslate">&lt;?php get_header()?&gt;</pre>
<p>并在下面添加</p>
<pre class="brush: css; title: ; notranslate">&lt;style type=&quot;text/css&quot;&gt;
#archives .al_mon {
	margin-left:3em;
}
#archives .al_post_list {
	margin-left:5.6em;
}
&lt;/style&gt;</pre>
<p></div></div>
<p>OK 大功告成了！现在你的博客也有jQuery伸缩效果的归档页面了，而且还是纯代码、原生态的。</p>
<p>也许有对代码头晕，但又想用归档页面的Mossight童鞋，好吧，那我好人做到底，将上面两个文件打包，提供给大家下载。<br />
<span style="color: #993300;"><strong>archives.php文件</strong></span><br />
使用方法：将该文件上传至Mossight文件夹即可（需要修改归档页面样式的童鞋，请在里面进行修改，具体位置已标注）<br />
<span class="but-down"><a href="http://shit.name/wp-content/uploads/2012/archives/archives.rar" target="_blank"><span>archives.php打包文件</span></a></span><br />
<span style="color: #993300;"><strong>functions.php文件</strong></span><br />
使用方法：将该文件覆盖Mossight-child文件夹里的同名文件即可<br />
<span class="but-down"><a href="http://shit.name/wp-content/uploads/2012/archives/functions.rar" target="_blank"><span>functions.php打包文件</span></a></span></p>
<blockquote><p><span style="color: #993300;"><strong>对于再次升级的Mossight1.8.0版主题也同样适用</strong></span><br />
再次感谢一直为该主题更新的三位童鞋   <A title="虾子酱" href="http://jezzzz.me/" target="_blank">Jezzzz</A>   <A title="沐歌" href="http://mugee.info/" target="_blank">Mugee</A>   <A title="牧风" href="http://mufeng.me" target="_blank">牧风</A></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://shit.name/pure-code-of-archives/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
	</channel>
</rss>
