<?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>睡到25点 &#187; 静态化</title>
	<atom:link href="http://www.voidman.com/tag/%e9%9d%99%e6%80%81%e5%8c%96/feed" rel="self" type="application/rss+xml" />
	<link>http://www.voidman.com</link>
	<description>个人博客，记录与分享，仅此而已。</description>
	<lastBuildDate>Fri, 30 Jul 2010 09:54:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>WordPress 分页文章静态化的更优解决方案</title>
		<link>http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html</link>
		<comments>http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html#comments</comments>
		<pubDate>Wed, 12 Nov 2008 13:19:46 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[URL Rewrite]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[分页文章]]></category>
		<category><![CDATA[分页日志]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://www.voidman.com/?p=179</guid>
		<description><![CDATA[<p class="page-links"><strong>Pages:</strong> 1 <a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post-2.html">2</a></p>之前用比较暴力的方式实现了分页文章的静态化，不过这样一来升级 WordPress 就不太方便了。厌烦了每次升级都要修改源文件，于是利用 WordPress 本身提供的接口实现了更好的解决方案。 以/%year%/%monthnum%/%postname%.html这样的永久链接结构为例： 1. 打开主题目录下的functions.php文件，添加以下代码： // 添加分页处理规则 function add_custom_post_rewrite_rules&#40;$rules&#41; &#123; $custom_rules = array&#40; '([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' =&#62; 'index.php?year=$matches[1]&#38;monthnum=$matches[2]&#38;name=$matches[3]&#38;page=$matches[4]', &#41;; $rules = array_merge&#40;$custom_rules, $rules&#41;; &#160; return $rules; &#125; add_filter&#40;'post_rewrite_rules', 'add_custom_post_rewrite_rules'&#41;; &#160; // 修改分页链接 function my_wp_link_pages&#40;$args = ''&#41; &#123; $args .= &#40;$args ? '&#38;' : ''&#41; . 'echo=0'; $links = wp_link_pages&#40;$args&#41;; $links = preg_replace_callback&#40;'&#124;([0-9]{4}/[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)&#124;', 'custom_page_link', $links&#41;; [...]]]></description>
			<content:encoded><![CDATA[<p class="page-links"><strong>Pages:</strong> 1 <a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post-2.html">2</a></p><p>之前用比较暴力的方式实现了<a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html">分页文章的静态化</a>，不过这样一来升级 WordPress 就不太方便了。厌烦了每次升级都要修改源文件，于是利用 WordPress 本身提供的接口实现了更好的解决方案。</p>
<p>以<code>/%year%/%monthnum%/%postname%.html</code>这样的永久链接结构为例：</p>
<p>1. 打开主题目录下的<code>functions.php</code>文件，添加以下代码：</p>

<div class="wp_syntax"><pre class="php"><span style="color: #008000; font-style: italic;">// 添加分页处理规则</span>
<span style="color: blue;">function</span> add_custom_post_rewrite_rules<span style="color: #800000;">&#40;</span><span style="color: #008080;">$rules</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: #008080;">$custom_rules</span> = <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span>
    <span style="color: #ff00ff;">'([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)<span style="">\.</span>html$'</span> =&gt; <span style="color: #ff00ff;">'index.php?year=$matches[1]&amp;monthnum=$matches[2]&amp;name=$matches[3]&amp;page=$matches[4]'</span>,
  <span style="color: #800000;">&#41;</span>;
  <span style="color: #008080;">$rules</span> = <span style="color: #ff0000;">array_merge</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$custom_rules</span>, <span style="color: #008080;">$rules</span><span style="color: #800000;">&#41;</span>;
&nbsp;
  <span style="color: blue;">return</span> <span style="color: #008080;">$rules</span>;
<span style="color: #800000;">&#125;</span>
add_filter<span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'post_rewrite_rules'</span>, <span style="color: #ff00ff;">'add_custom_post_rewrite_rules'</span><span style="color: #800000;">&#41;</span>;
&nbsp;
<span style="color: #008000; font-style: italic;">// 修改分页链接</span>
<span style="color: blue;">function</span> my_wp_link_pages<span style="color: #800000;">&#40;</span><span style="color: #008080;">$args</span> = <span style="color: #ff00ff;">''</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: #008080;">$args</span> .= <span style="color: #800000;">&#40;</span><span style="color: #008080;">$args</span> ? <span style="color: #ff00ff;">'&amp;'</span> : <span style="color: #ff00ff;">''</span><span style="color: #800000;">&#41;</span> . <span style="color: #ff00ff;">'echo=0'</span>;
  <span style="color: #008080;">$links</span> = wp_link_pages<span style="color: #800000;">&#40;</span><span style="color: #008080;">$args</span><span style="color: #800000;">&#41;</span>;
  <span style="color: #008080;">$links</span> = <span style="color: #ff0000;">preg_replace_callback</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'|([0-9]{4}/[0-9]{1,2}/)([^/]+)(<span style="">\.</span>html)(/)([0-9]+)|'</span>, <span style="color: #ff00ff;">'custom_page_link'</span>, <span style="color: #008080;">$links</span><span style="color: #800000;">&#41;</span>;
&nbsp;
  <span style="color: blue;">echo</span> <span style="color: #008080;">$links</span>;
<span style="color: #800000;">&#125;</span>
&nbsp;
<span style="color: blue;">function</span> custom_page_link<span style="color: #800000;">&#40;</span><span style="color: #008080;">$matches</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: blue;">return</span> <span style="color: #008080;">$matches</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">1</span><span style="color: #800000;">&#93;</span>.<span style="color: #008080;">$matches</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">2</span><span style="color: #800000;">&#93;</span>.<span style="color: #ff00ff;">'-'</span>.<span style="color: #008080;">$matches</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">5</span><span style="color: #800000;">&#93;</span>.<span style="color: #008080;">$matches</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">3</span><span style="color: #800000;">&#93;</span>;
<span style="color: #800000;">&#125;</span></pre></div>

<p><span id="more-179"></span><br />
2. 打开主题目录下的<code>single.php</code>文件，查找<code>wp_link_pages</code>并替换为<code>my_wp_link_pages</code>。</p>
<p>3. 后台“设置-永久链接”点击一下“保存修改”按钮，大功告成。</p>
<p>至于 life97 同学说的 <a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html#comment-3781">.html/xxx 形式的链接访问失效</a>，这个可能和服务器设置有关，具体原因我也不是很清楚，不过可以通过在 .htaccess 文件中添加规则来解决此问题，如：</p>

<div class="wp_syntax"><pre class="apache">&lt;ifmodule mod_rewrite.c&gt;
<span style="color: #00007f;">RewriteEngine</span> <span style="color: #0000ff;">On</span>
<span style="color: #00007f;">RewriteBase</span> /
<span style="color: #00007f;">RewriteRule</span> ^<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">4</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span>^/<span style="color: #800000;">&#93;</span>+<span style="color: #800000;">&#41;</span>\.html/trackback/?$ index.php?year=$<span style="color: #ff0000;">1</span>&amp;monthnum=$<span style="color: #ff0000;">2</span>&amp;name=$<span style="color: #ff0000;">3</span>&amp;tb=<span style="color: #ff0000;">1</span> <span style="color: #800000;">&#91;</span>L<span style="color: #800000;">&#93;</span>
<span style="color: #00007f;">RewriteRule</span> ^<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">4</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span>^/<span style="color: #800000;">&#93;</span>+<span style="color: #800000;">&#41;</span>\.html/feed/<span style="color: #800000;">&#40;</span>feed|rdf|rss|rss2|atom<span style="color: #800000;">&#41;</span>/?$ index.php?year=$<span style="color: #ff0000;">1</span>&amp;monthnum=$<span style="color: #ff0000;">2</span>&amp;name=$<span style="color: #ff0000;">3</span>&amp;feed=$<span style="color: #ff0000;">4</span> <span style="color: #800000;">&#91;</span>L<span style="color: #800000;">&#93;</span>
<span style="color: #00007f;">RewriteRule</span> ^<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">4</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span><span style="color: #ff0000;">0</span><span style="color: #ff0000;">-9</span><span style="color: #800000;">&#93;</span><span style="color: #800000;">&#123;</span><span style="color: #ff0000;">1</span>,<span style="color: #ff0000;">2</span><span style="color: #800000;">&#125;</span><span style="color: #800000;">&#41;</span>/<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#91;</span>^/<span style="color: #800000;">&#93;</span>+<span style="color: #800000;">&#41;</span>\.html/<span style="color: #800000;">&#40;</span>feed|rdf|rss|rss2|atom<span style="color: #800000;">&#41;</span>/?$ index.php?year=$<span style="color: #ff0000;">1</span>&amp;monthnum=$<span style="color: #ff0000;">2</span>&amp;name=$<span style="color: #ff0000;">3</span>&amp;feed=$<span style="color: #ff0000;">4</span> <span style="color: #800000;">&#91;</span>L<span style="color: #800000;">&#93;</span>
&lt;/ifmodule&gt;</pre></div>

<p>注意添加顺序，不要置于 WordPress 生成的规则之后。</p>
<p>下面是几种其它永久链接结构的具体修改代码：</p>
<ul>
<li><a href="http://img.voidman.com/wp/2008/11/year-monthnumday-postname.txt">/%year%/%monthnum%%day%/%postname%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/html-year-monthnum-day-postname.txt">/html/%year%/%monthnum%/%day%/%postname%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/category-year-monthnum-day-post_id.txt">/%category%/%year%/%monthnum%/%day%/%post_id%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/year-monthnumday-post_id.txt">/%year%/%monthnum%%day%/%post_id%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/html-post_id.txt">/html/%post_id%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/html-postname.txt">/html/%postname%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/post-postname.txt">/post/%postname%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/postname.txt">/%postname%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/category-post_id.txt">/%category%-%post_id%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/category-post_id2.txt">/%category%/%post_id%.html</a></li>
<li><a href="http://img.voidman.com/wp/2008/11/category-postname.txt">/%category%/%postname%.html</a></li>
</ul>
<p><strong>UPDATE at 2009-4-1</strong><br />
添加<code>/%year%/%monthnum%%day%/%post_id%.html</code>与<code>/html/%post_id%.html</code>这两种永久链接结构的修改代码</p>
<p><strong>UPDATE at 2009-4-25</strong><br />
添加<code>/%category%-%post_id%.html</code>永久链接结构的修改代码</p>
<p><strong>UPDATE at 2009-5-31</strong><br />
添加<code>/%category%/%postname%.html</code>永久链接结构的修改代码</p>
<p><strong>UPDATE at 2009-9-30</strong><br />
添加<code>/%category%/%year%/%monthnum%/%day%/%post_id%.html</code>永久链接结构的修改代码</p>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html" title="让 Wordpress 分页文章也可以静态化">让 Wordpress 分页文章也可以静态化</a></li><li><a href="http://www.voidman.com/2008/03/trouble-with-static-wordpress.html" title="Wordpress 静态化的烦恼">Wordpress 静态化的烦恼</a></li><li><a href="http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html" title="让 GZIP 与 cos-html-cache 共存">让 GZIP 与 cos-html-cache 共存</a></li><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html" title="Wordpress 糟糕的重复评论检测方式">Wordpress 糟糕的重复评论检测方式</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html" title="让 Wordpress 自动删除 Post Revisions">让 Wordpress 自动删除 Post Revisions</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html">http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html/feed</wfw:commentRss>
		<slash:comments>73</slash:comments>
		</item>
		<item>
		<title>WordPress 静态化的烦恼</title>
		<link>http://www.voidman.com/2008/03/trouble-with-static-wordpress.html</link>
		<comments>http://www.voidman.com/2008/03/trouble-with-static-wordpress.html#comments</comments>
		<pubDate>Sat, 15 Mar 2008 09:31:02 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://www.voidman.com/2008/03/trouble-with-static-wordpress.html</guid>
		<description><![CDATA[其实，确切地说应该是前几日的烦恼了。因为将永久链接结构设置成以下形式的关系，在静态化页面后，由于目录冲突，WP 便无法按年月浏览了。 /%year%/%monthnum%/%postname%.html 起初将静态化页面生成的年份、月份目录设置成可以列表，不至于出现浏览错误。不过这样一来，侧栏的日历以及按月归档功能便如同鸡肋了。 注意到若将永久链接结构设置设置成“使用日志的 ID 号”结构，按日期浏览时，WP 会自动加上前缀“date”，这是为了防止解析页面地址的时候将日志 ID 号和日期混淆起来。于是我想当然地将永久链接结构修改成： /archives/%year%/%monthnum%/%postname%.html 以为可以解决问题。不料按年月浏览时，WP 居然会自动将地址修改为以下形式，也就说仍旧会产生目录冲突： /archives/%year%/%monthnum%/ 如此一来，只好再继续暴力一把了。 打开 rewrite.php 文件，找到 foreach &#40;$tokens&#91;0&#93; as $token&#41; &#123; if &#40; &#40;&#40;$token == '%post_id%'&#41; &#38;&#38; &#40;$tok_index &#60;= 3&#41;&#41; &#41; &#123; $front = $front . 'date/'; break; &#125; $tok_index++; &#125; 修改成 foreach &#40;$tokens&#91;0&#93; as $token&#41; &#123; if &#40; &#40;&#40;$token == '%post_id%'&#41; &#38;&#38; [...]]]></description>
			<content:encoded><![CDATA[<p>其实，确切地说应该是前几日的烦恼了。因为将永久链接结构设置成以下形式的关系，在静态化页面后，由于目录冲突，WP 便无法按年月浏览了。</p>

<div class="wp_syntax"><pre>/%year%/%monthnum%/%postname%.html</pre></div>

<p>起初将静态化页面生成的年份、月份目录设置成可以列表，不至于出现浏览错误。不过这样一来，侧栏的日历以及按月归档功能便如同鸡肋了。</p>
<p>注意到若将永久链接结构设置设置成“使用日志的 ID 号”结构，按日期浏览时，WP 会自动加上前缀“date”，这是为了防止解析页面地址的时候将日志 ID 号和日期混淆起来。于是我想当然地将永久链接结构修改成：</p>

<div class="wp_syntax"><pre>/archives/%year%/%monthnum%/%postname%.html</pre></div>

<p>以为可以解决问题。不料按年月浏览时，WP 居然会自动将地址修改为以下形式，也就说仍旧会产生目录冲突：</p>

<div class="wp_syntax"><pre>/archives/%year%/%monthnum%/</pre></div>

<p>如此一来，只好再继续<a href="/2008/03/create-static-html-files-for-paged-post.html">暴力一把</a>了。<br />
<span id="more-146"></span><br />
打开 rewrite.php 文件，找到</p>

<div class="wp_syntax"><pre class="php"><span style="color: blue;">foreach</span> <span style="color: #800000;">&#40;</span><span style="color: #008080;">$tokens</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">0</span><span style="color: #800000;">&#93;</span> <span style="color: blue;">as</span> <span style="color: #008080;">$token</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #800000;">&#40;</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$token</span> == <span style="color: #ff00ff;">'%post_id%'</span><span style="color: #800000;">&#41;</span> &amp;&amp; <span style="color: #800000;">&#40;</span><span style="color: #008080;">$tok_index</span> &lt;= <span style="color: #800080;">3</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
    <span style="color: #008080;">$front</span> = <span style="color: #008080;">$front</span> . <span style="color: #ff00ff;">'date/'</span>;
    <span style="color: blue;">break</span>;
  <span style="color: #800000;">&#125;</span>
  <span style="color: #008080;">$tok_index</span>++;
<span style="color: #800000;">&#125;</span></pre></div>

<p>修改成</p>

<div class="wp_syntax"><pre class="php"><span style="color: blue;">foreach</span> <span style="color: #800000;">&#40;</span><span style="color: #008080;">$tokens</span><span style="color: #800000;">&#91;</span><span style="color: #800080;">0</span><span style="color: #800000;">&#93;</span> <span style="color: blue;">as</span> <span style="color: #008080;">$token</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #800000;">&#40;</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$token</span> == <span style="color: #ff00ff;">'%post_id%'</span><span style="color: #800000;">&#41;</span> &amp;&amp; <span style="color: #800000;">&#40;</span><span style="color: #008080;">$tok_index</span> &lt;= <span style="color: #800080;">3</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span> || <span style="color: #800000;">&#40;</span> <span style="color: #008080;">$token</span> == <span style="color: #ff00ff;">'%year%'</span> &amp;&amp; <span style="color: #008080;">$tok_index</span> == <span style="color: #800080;">1</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
    <span style="color: #008080;">$front</span> = <span style="color: #008080;">$front</span> . <span style="color: #ff00ff;">'date/'</span>;
    <span style="color: blue;">break</span>;
  <span style="color: #800000;">&#125;</span>
  <span style="color: #008080;">$tok_index</span>++;
<span style="color: #800000;">&#125;</span></pre></div>

<p>一番折腾，总算解决问题。</p>
<p>为了将 WP 静态化，已经不止一次遇到问题了，也许还会遇到更多不可预知的问题。想想，折腾，有时候也算是一种乐趣吧。</p>
<blockquote><p>
已经有了更好的解决方案，<a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post-2.html">点此查看</a>
</p></blockquote>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html" title="Wordpress 分页文章静态化的更优解决方案">Wordpress 分页文章静态化的更优解决方案</a></li><li><a href="http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html" title="让 GZIP 与 cos-html-cache 共存">让 GZIP 与 cos-html-cache 共存</a></li><li><a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html" title="让 Wordpress 分页文章也可以静态化">让 Wordpress 分页文章也可以静态化</a></li><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html" title="Wordpress 糟糕的重复评论检测方式">Wordpress 糟糕的重复评论检测方式</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html" title="让 Wordpress 自动删除 Post Revisions">让 Wordpress 自动删除 Post Revisions</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2008/03/trouble-with-static-wordpress.html">http://www.voidman.com/2008/03/trouble-with-static-wordpress.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2008/03/trouble-with-static-wordpress.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>让 GZIP 与 cos-html-cache 共存</title>
		<link>http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html</link>
		<comments>http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html#comments</comments>
		<pubDate>Fri, 14 Mar 2008 07:07:09 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[cos-html-cache]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html</guid>
		<description><![CDATA[用过 cos-html-cache 插件的人都知道，cos-html-cache 需要在 WordPress 后台关闭 gzip 压缩选项才能正常工作。因为 cos-html-cache 只静态化首页和日志页面，这样一来像分类浏览页面、按日期浏览页面、搜索结果页面等等既没有静态化，也没有享受到 gzip 压缩带来的好处。 其实有时候鱼和熊掌还是可以兼得的。Wordpress 后台的 gzip 压缩设置是个全局选项，它不区分页面类型，那么我们可以手动对需要压缩的页面启用 gzip 。将以下代码添加到主题目录下的 functions.php header.php 文件中的 HTML 代码之前： &#60;?php // 下面的代码大部分来自 Wordpress 的 gzip_compression 函数 // 如果关闭了 gzip 并且启用了 cos-html-cache 插件 if &#40; !get_option&#40; 'gzipcompression' &#41; &#38;&#38; function_exists&#40;'CosSafeTag'&#41;&#41; &#123; // 如果不是日志页或者首页 if&#40;!&#40;is_single&#40;&#41; &#124;&#124; &#40;is_home&#40;&#41; &#38;&#38; !is_paged&#40;&#41;&#41;&#41;&#41;&#123; if &#40; &#40; ini_get&#40; [...]]]></description>
			<content:encoded><![CDATA[<p>用过 <a href="http://www.storyday.com/html/y2008/1446_cos-html-cache-upgrade.html" rel="external nofollow">cos-html-cache</a> 插件的人都知道，cos-html-cache 需要在 WordPress 后台关闭 gzip 压缩选项才能正常工作。因为 cos-html-cache 只静态化首页和日志页面，这样一来像分类浏览页面、按日期浏览页面、搜索结果页面等等既没有静态化，也没有享受到 gzip 压缩带来的好处。</p>
<p>其实有时候鱼和熊掌还是可以兼得的。Wordpress 后台的 gzip 压缩设置是个全局选项，它不区分页面类型，那么我们可以手动对需要压缩的页面启用 gzip 。将以下代码添加到主题目录下的 <del datetime="20080315">functions.php</del> header.php 文件中的 HTML 代码之前：</p>

<div class="wp_syntax"><pre class="php"><span style="color: blue;">&lt;?php</span>
<span style="color: #008000; font-style: italic;">// 下面的代码大部分来自 Wordpress 的 gzip_compression 函数</span>
<span style="color: #008000; font-style: italic;">// 如果关闭了 gzip 并且启用了 cos-html-cache 插件</span>
<span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> !get_option<span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'gzipcompression'</span> <span style="color: #800000;">&#41;</span> &amp;&amp; <span style="color: #ff0000;">function_exists</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'CosSafeTag'</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: #008000; font-style: italic;">// 如果不是日志页或者首页</span>
  <span style="color: blue;">if</span><span style="color: #800000;">&#40;</span>!<span style="color: #800000;">&#40;</span>is_single<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#41;</span> || <span style="color: #800000;">&#40;</span>is_home<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#41;</span> &amp;&amp; !is_paged<span style="color: #800000;">&#40;</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#123;</span>
    <span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #800000;">&#40;</span> <span style="color: #ff0000;">ini_get</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'zlib.output_compression'</span> <span style="color: #800000;">&#41;</span> == <span style="color: #ff00ff;">'On'</span> || <span style="color: #ff0000;">ini_get</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'zlib.output_compression_level'</span> <span style="color: #800000;">&#41;</span> &gt; <span style="color: #800080;">0</span> <span style="color: #800000;">&#41;</span> || <span style="color: #ff0000;">ini_get</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'output_handler'</span> <span style="color: #800000;">&#41;</span> == <span style="color: #ff00ff;">'ob_gzhandler'</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
      <span style="color: #008000; font-style: italic;">// Do nothing</span>
    <span style="color: #800000;">&#125;</span> <span style="color: blue;">else</span> <span style="color: #800000;">&#123;</span>
      <span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span> <span style="color: #ff0000;">extension_loaded</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'zlib'</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
        <span style="color: #ff0000;">ob_start</span><span style="color: #800000;">&#40;</span> <span style="color: #ff00ff;">'ob_gzhandler'</span> <span style="color: #800000;">&#41;</span>;
      <span style="color: #800000;">&#125;</span>
    <span style="color: #800000;">&#125;</span>
  <span style="color: #800000;">&#125;</span>
<span style="color: #800000;">&#125;</span>
<span style="color: blue;">?&gt;</span></pre></div>

<p>本来也没想到要这么做，因为<a href="http://www.voidman.com/2008/03/wordpress-404-page-not-work-well-in-ie.html">折腾 WordPress 的 404 页面</a>过程中发现打开 gzip 后 IE 就可以正常显示错误页面，索性将其它没有被静态化的页面一并压缩了。</p>
<p><strong>UPDATE</strong> at 2008-3-15 21:37<br />
刚才再次测试的时候才发现上面那段代码放在 functions.php 中会工作不正常，因为 WP 加载 functions.php 的时候还没执行查询操作，is_single() 判断根本就不起作用。惭愧，有些粗心大意了。</p>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html" title="Wordpress 分页文章静态化的更优解决方案">Wordpress 分页文章静态化的更优解决方案</a></li><li><a href="http://www.voidman.com/2008/03/trouble-with-static-wordpress.html" title="Wordpress 静态化的烦恼">Wordpress 静态化的烦恼</a></li><li><a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html" title="让 Wordpress 分页文章也可以静态化">让 Wordpress 分页文章也可以静态化</a></li><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html" title="Wordpress 糟糕的重复评论检测方式">Wordpress 糟糕的重复评论检测方式</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html" title="让 Wordpress 自动删除 Post Revisions">让 Wordpress 自动删除 Post Revisions</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html">http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>让 WordPress 分页文章也可以静态化</title>
		<link>http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html</link>
		<comments>http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html#comments</comments>
		<pubDate>Thu, 06 Mar 2008 16:53:06 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Blog Related]]></category>
		<category><![CDATA[URL Rewrite]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html</guid>
		<description><![CDATA[WordPress 提供了多种结构标签，以便我们可以设置各种格式的永久链接结构，再配合一些静态化插件（例如 cos-html-cache），就可以使页面真正静态化。 不过 WordPress 对已分页文章的永久链接的处理方式则会给页面静态化后的访问带来问题。 例如，永久链接结构为 /%year%/%monthnum%/%postname%.html，WordPress 生成的文章相关分页链接如下所示： yourdomain.com/2008/03/postname.html yourdomain.com/2008/03/postname.html/2 yourdomain.com/2008/03/postname.html/3 可以看到 WordPress 只是简单地将页码加在了链接尾部，所以当我们静态化其中一页的内容后，我们将只能访问被静态化的那一页内容而无法访问其它分页的内容。为了可以静态化所有分页内容，需要对 WordPress 处理永久链接的方式做些小小的改动，并改变分页链接的形式： yourdomain.com/2008/03/postname.html yourdomain.com/2008/03/postname-2.html yourdomain.com/2008/03/postname-3.html 以前面提到的永久链接结构为例，作如下修改： /* 打开 wp-includes/rewrite.php 文件 找到 $rewrite = array_merge($rewrite, array($match =&#62; $query)) 在这行代码之前添加以下代码： */ if &#40;strpos&#40;$match, '.html'&#41; !== false &#38;&#38; strpos&#40;$query, '&#38;page='&#41; !== false&#41; &#123; $match = str_replace&#40;'(/[0-9]+)?/?$', '$', $match&#41;; $rewrite = array_merge&#40;$rewrite, array&#40;str_replace&#40;'([^/]+).html', '([^/]+)-([0-9]+).html', [...]]]></description>
			<content:encoded><![CDATA[<p>WordPress 提供了多种<a href="http://codex.wordpress.org/Using_Permalinks" >结构标签</a>，以便我们可以设置各种格式的永久链接结构，再配合一些静态化插件（例如 <a href="http://www.storyday.com/html/y2008/1446_cos-html-cache-upgrade.html" >cos-html-cache</a>），就可以使页面真正静态化。</p>
<p>不过 WordPress 对<a href="http://codex.wordpress.org/Styling_Page-Links" >已分页文章</a>的永久链接的处理方式则会给页面静态化后的访问带来问题。 例如，永久链接结构为 <code>/%year%/%monthnum%/%postname%.html</code>，WordPress 生成的文章相关分页链接如下所示：</p>

<div class="wp_syntax"><pre>yourdomain.com/2008/03/postname.html 
yourdomain.com/2008/03/postname.html/2 
yourdomain.com/2008/03/postname.html/3</pre></div>

<p>可以看到 WordPress 只是简单地将页码加在了链接尾部，所以当我们静态化其中一页的内容后，我们将只能访问被静态化的那一页内容而无法访问其它分页的内容。为了可以静态化所有分页内容，需要对 WordPress 处理永久链接的方式做些小小的改动，并改变分页链接的形式：</p>

<div class="wp_syntax"><pre>yourdomain.com/2008/03/postname.html 
yourdomain.com/2008/03/postname-2.html
yourdomain.com/2008/03/postname-3.html</pre></div>

<p><span id="more-136"></span><br />
以前面提到的永久链接结构为例，作如下修改：</p>

<div class="wp_syntax"><pre class="php"><span style="color: #008000; font-style: italic;">/*
  打开 wp-includes/rewrite.php 文件
  找到 $rewrite = array_merge($rewrite, array($match =&gt; $query))
  在这行代码之前添加以下代码：
*/</span>
<span style="color: blue;">if</span> <span style="color: #800000;">&#40;</span><span style="color: #ff0000;">strpos</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$match</span>, <span style="color: #ff00ff;">'.html'</span><span style="color: #800000;">&#41;</span> !== <span style="color: blue;">false</span> &amp;&amp; <span style="color: #ff0000;">strpos</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$query</span>, <span style="color: #ff00ff;">'&amp;page='</span><span style="color: #800000;">&#41;</span> !== <span style="color: blue;">false</span><span style="color: #800000;">&#41;</span> <span style="color: #800000;">&#123;</span>
  <span style="color: #008080;">$match</span> = <span style="color: #ff0000;">str_replace</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'(/[0-9]+)?/?$'</span>, <span style="color: #ff00ff;">'$'</span>, <span style="color: #008080;">$match</span><span style="color: #800000;">&#41;</span>;
  <span style="color: #008080;">$rewrite</span> = <span style="color: #ff0000;">array_merge</span><span style="color: #800000;">&#40;</span><span style="color: #008080;">$rewrite</span>, <span style="color: blue;">array</span><span style="color: #800000;">&#40;</span><span style="color: #ff0000;">str_replace</span><span style="color: #800000;">&#40;</span><span style="color: #ff00ff;">'([^/]+).html'</span>, <span style="color: #ff00ff;">'([^/]+)-([0-9]+).html'</span>, <span style="color: #008080;">$match</span><span style="color: #800000;">&#41;</span> =&gt; <span style="color: #008080;">$query</span><span style="color: #800000;">&#41;</span><span style="color: #800000;">&#41;</span>;
<span style="color: #800000;">&#125;</span></pre></div>

<p>以上代码使 WordPress 可以处理第一页后面的分页链接。其实通过修改 <code>.htaccess</code> 也可以达到同样的效果。</p>
<p>除此之外还需要修改文章分页链接生成模板。打开 <code>wp-includes/post-template.php</code> 文件，修改 <code>wp_link_pages</code> 函数（这段代码不太好贴，<a href='http://www.voidman.com/uploads/wp/2008/03/wp_link_pages.phps' title='wp_link_pages.phps'>点此查看</a>）。</p>
<p>具体效果，可以点击<a href="http://www.voidman.com/2008/01/the-journey-of-beijing.html">这里</a>还有<a href="http://www.voidman.com/2008/02/camellia-and-wintersweet.html">这里</a>查看。</p>
<p><strong>UPDATE:</strong><br />
已经有了更好的解决方案，<a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html">点此查看</a></p>
<h4>Related Posts</h4><ul class="related_post"><li><a href="http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html" title="Wordpress 分页文章静态化的更优解决方案">Wordpress 分页文章静态化的更优解决方案</a></li><li><a href="http://www.voidman.com/2008/03/trouble-with-static-wordpress.html" title="Wordpress 静态化的烦恼">Wordpress 静态化的烦恼</a></li><li><a href="http://www.voidman.com/2008/03/wordpress-can-coexist-with-gizp-and-cos-html-cache.html" title="让 GZIP 与 cos-html-cache 共存">让 GZIP 与 cos-html-cache 共存</a></li><li><a href="http://www.voidman.com/2009/05/using-wordpress-plugin-api-hack-discuz.html" title="WordPress 的 Hook 机制在 Discuz 二次开发中的应用">WordPress 的 Hook 机制在 Discuz 二次开发中的应用</a></li><li><a href="http://www.voidman.com/2009/04/at-reply-mail-notification-1-release.html" title="At Reply Mail Notification 1.0 Release">At Reply Mail Notification 1.0 Release</a></li><li><a href="http://www.voidman.com/2009/03/lbs-to-wordpress.html" title="LBS 转 WordPress 不完全记录">LBS 转 WordPress 不完全记录</a></li><li><a href="http://www.voidman.com/2009/01/wordpress-is-using-a-bad-way-to-detect-duplicate-comment.html" title="Wordpress 糟糕的重复评论检测方式">Wordpress 糟糕的重复评论检测方式</a></li><li><a href="http://www.voidman.com/2009/01/at-reply-mail-notification-test.html" title="At Reply Mail Notification 测试贴">At Reply Mail Notification 测试贴</a></li><li><a href="http://www.voidman.com/2008/11/automatically-delete-post-revisions.html" title="让 Wordpress 自动删除 Post Revisions">让 Wordpress 自动删除 Post Revisions</a></li><li><a href="http://www.voidman.com/2008/11/google-xml-sitemaps-ignore-the-specified-categories.html" title="让 Google Sitemaps Generator 可以忽略指定的日志分类">让 Google Sitemaps Generator 可以忽略指定的日志分类</a></li></ul><hr /><a href="http://www.voidman.com">Voidman.com</a> | Link: <a href="http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html">http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html</a><br /><img alt="linezing" width="1" height="1" src="http://img.tongji.linezing.com/288120/tongji.gif" />]]></content:encoded>
			<wfw:commentRss>http://www.voidman.com/2008/03/create-static-html-files-for-paged-post.html/feed</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
	</channel>
</rss>
