WordPress单篇文章分页静态化错误的处理方法

      原帖位置(我增加了一种自己使用的方式):http://www.voidman.com/2008/11/the-better-solution-for-static-paged-post.html

      WordPress 提供了多种结构标签,以便我们可以设置各种格式的永久链接结构,再配合一些静态化插件(例如 cos-html-cache),就可以使页面真正静态化。
      不过 WordPress 对已分页文章的永久链接的处理方式则会给页面静态化后的访问带来问题。 例如,永久链接结构为 /html/%year%/%monthnum%/%post_id%.html,WordPress 生成的文章相关分页链接如下所示:

            yourdomain.com/html/2009/05/126.html
            yourdomain.com/html/2009/05/126.html/2
             yourdomain.com/html/2009/05/126.html/3

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

            yourdomain.com/html/2009/05/126.html
            yourdomain.com/html/2009/05/126-2.html
            yourdomain.com/html/2009/05/126-3.html

下面谈谈使用方法(这种方法是目前最好的方法,wordpress主程序升级、主题不升级不会受影响,在wordpress2.7.1、3.1.2、3.1.3上通过并使用着):以 /html/%year%/%monthnum%/%post_id%.html 这样的永久链接结构为例:

1. 打开主题目录下的functions.php文件,在尾部的“?>”前面添加以下代码:



// 添加分页处理规则
function add_custom_post_rewrite_rules($rules) {
  $custom_rules = array(
    'html/([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&p=$matches[3]&page=$matches[4]',
  );
  $rules = array_merge($custom_rules, $rules);
  return $rules;
}
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
// 修改分页链接
function my_wp_link_pages($args = '') {
  $args .= ($args ? '&' : '') . 'echo=0';
  $links = wp_link_pages($args);
  $links = preg_replace_callback('|(html/[0-9]{4}/[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', 

$links);
  echo $links;
}
function custom_page_link($matches) {
  return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
}

2. 打开主题目录下的single.php文件,查找wp_link_pages并替换为my_wp_link_pages

3. 后台“设置-永久链接”点击一下“保存修改”按钮,大功告成。

至于有朋友出现 .html/xxx 形式的链接访问失效,可能和服务器设置有关,可以通过在 .htaccess 文件中添加规则来解决此问题,如:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^html/([0-9]{4})/([0-9]{1,2})/([^/]+)\.html/trackback/?$ index.php?year=$1&monthnum=$2&p=$3&tb=1 [L]
RewriteRule ^html/([0-9]{4})/([0-9]{1,2})/([^/]+)\.html/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&p=$3&feed=$4 [L]
RewriteRule ^html/([0-9]{4})/([0-9]{1,2})/([^/]+)\.html/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&p=$3&feed=$4 [L]
RewriteRule ^html/([0-9]{4})/([0-9]{1,2})/([^/]+)\.html/page/?([0-9]{1,})/?$ index.php?year=$1&monthnum=$2&p=$3&paged=$4
RewriteRule ^html/([0-9]{4})/([0-9]{1,2})/([^/]+)\.html/([0-9]+)/?$ index.php?year=$1&monthnum=$2&p=$3&page=$4
</IfModule>

注意添加顺序,不要置于 WordPress 生成的规则之后。


0%(0)

100%(3)
1 2 3 4下一页
发表评论?

25 条评论。

  1. 博主您好 请问你的博客主题用的是那一款主题.谢谢

  2. 博主,我的网站可以和你交换链接吗?

  3. 第一次到访你的博客,文章写的很不错,希望博主也到我网站回访。

  4. 我的文章都不长,所以无须分页!

  5. po主,我按照上面的设置了,一步步设置,一步没差;
    href链接显示正常,一点击(或者手动输入)就跳转了
    http://127.0.0.1/html/2013/05/19/xxx-3.html
    跳为:
    http://127.0.0.1/html/2013/05/19/xxx.html/3
    希望po主指教下

  6. 我的博客写了几天就散伙了,实在没什么好写的,看了你的博客才知道,我只是个打酱油滴!

  7. 写的很不错,学习了。

  8. 这么长的文章不用分页也不行了….分页也是个杯具,

    我还是累点,自己手动分页

发表评论

*