现在的位置: 首页Php>正文
WordPress 分页文章静态化修正
2012年02月19日 Php 评论数 18 ⁄ 被围观 45,524 次+

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

  1. yourdomain.com/2008/03/postname.html    
  2. yourdomain.com/2008/03/postname.html/2    
  3. yourdomain.com/2008/03/postname.html/3  

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

  1. yourdomain.com/2008/03/postname.html   
  2. yourdomain.com/2008/03/postname_2.html   
  3. yourdomain.com/2008/03/postname_3.html  

于是利用 WordPress 本身提供的接口实现了更好的解决方案。
以/%year%/%monthnum%/%postname%.html这样的永久链接结构为例:

1. 打开主题目录下的functions.php文件,添加以下代码:

  1. // 添加分页处理规则   
  2. function add_custom_post_rewrite_rules($rules) {   
  3.     $custom_rules = array(   
  4.         '([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&name=$matches[3]&page=$matches[4]',   
  5.     );   
  6.     $rules = array_merge($custom_rules$rules);   
  7.   
  8.     return $rules;   
  9. }   
  10. add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');   
  11.     
  12. // 修改分页链接   
  13. function my_wp_link_pages($args = '') {   
  14.     $args .= ($args ? '&' : '') . 'echo=0';   
  15.     $links = wp_link_pages($args);   
  16.     $links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);   
  17.   
  18.     echo $links;   
  19. }   
  20.     
  21. function custom_page_link($matches) {   
  22.     return $matches[1].$matches[2].'-'.$matches[5].$matches[3];   
  23. }  

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

3. 后台“设置-永久链接”点击一下“保存修改”按钮,大功告成。具体效果可以看本文
下面是几种其它永久链接结构的具体修改代码:

  1. 固定链接格式为:/%year%/%monthnum%%day%/%postname%.html  
  1. // 添加分页处理规则   
  2. function add_custom_post_rewrite_rules($rules) {   
  3.     $custom_rules = array(   
  4.         '([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&name=$matches[4]&page=$matches[5]',   
  5.     );   
  6.     $rules = array_merge($custom_rules$rules);   
  7.   
  8.     return $rules;   
  9. }   
  10. add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');   
  11.   
  12. // 修改分页链接   
  13. function my_wp_link_pages($args = '') {   
  14.     $args .= ($args ? '&' : '') . 'echo=0';   
  15.     $links = wp_link_pages($args);   
  16.     $links = preg_replace_callback('|([0-9]{4}/[0-9]{1,2}[0-9]{1,2}/)([^/]+)(\.html)(/)([0-9]+)|', 'custom_page_link', $links);   
  17.   
  18.     echo $links;   
  19. }   
  20.   
  21. function custom_page_link($matches) {   
  22.     return $matches[1].$matches[2].'-'.$matches[5].$matches[3];   
  23. }  

相关Apache下的RewriteRule

  1. <IfModule mod_rewrite.c>   
  2. RewriteEngine On   
  3. RewriteBase /   
  4. RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/trackback/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&tb=1 [L]   
  5. RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/feed/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [L]   
  6. RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/(feed|rdf|rss|rss2|atom)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&feed=$5 [L]   
  7. RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/page/?([0-9]{1,})/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&paged=$5  
  8. RewriteRule ^([0-9]{4})/([0-9]{1,2})([0-9]{1,2})/([^/]+)\.html/([0-9]+)/?$ index.php?year=$1&monthnum=$2&day=$3&name=$4&page=$5  
  9. </IfModule>  
本文地址:http://www.92csz.com/41/1065.html
如非注明则为本站原创文章,欢迎转载。转载请注明转载自:moon's blog

1 2 3 4 5 下一页

目前有 18 条留言 其中:访客:11 条, 博主:7 条

  1. 朱定聪的博客 : 2012年02月22日06:29:32  -49楼

    我的robots都是直接隐蔽/data/,也不知道有没有必要~~

  2. 手机界面设计 : 2012年04月20日10:12:29  -48楼

    正是需要的,谢谢博主分享。

  3. ycqj : 2012年10月05日14:39:22  -47楼

    页面下方的页面链接显示“http://www.m5050.com/freeziyuan/453-2.html”
    但打开时显示http://www.m5050.com/freeziyuan/453.html/2
    这是为什么啊? 😥

    • clairelume : 2012年10月08日12:44:33

      固定链接格式设置对吗?检查下后台固定链接的格式

    • Mee : 2012年11月14日15:04:30

      我的也是 求解释

      • clairelume : 2012年11月21日12:33:51

        查看一下你后台固定链接格式设置的对吗?这个是要改的 😛

        • realhood : 2013年05月19日23:38:44

          【状况同上】固定链接设置对了
          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主指教下 😥
          "\""""\""""\"""

          • clairelume : 2013年05月20日17:05:28

            2. 打开主题目录下的single.php文件,查找wp_link_pages并替换为my_wp_link_pages这步确定没有少吗,鼠标放到分页上看下链接对不对 😳

            • realhood : 2013年05月21日12:42:28

              鼠标放在链接上显示的是对的,xxx-3.html格式,就是一点击就跳了,
              诡异的是当我把后台的固定链接给为/p?=123的格式,虽然链接不显示静态格式,但是手动输入却能正常按照xxx-3.html的格式访问。

              • clairelume : 2013年05月24日17:10:41

                还真是诡异啊,你用的什么web服务器,apahce还nginx,你是不是配置了伪静态了

                • realhood : 2013年06月01日02:00:16

                  apahce,配置了伪静态,请问您的wordpress 什么版本?我的是最新版本 ❓

              • clairelume : 2013年06月17日17:36:29

                我apache和nginx都用过,都可以用啊,不用配置伪静态规则,我的.htaccess里也没什么
                # BEGIN WordPress
                <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteBase /
                RewriteRule ^index\.php$ – [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
                </IfModule>

                # END WordPress
                用nginx时也就配置了
                location /
                {
                if (!-e $request_filename)
                {
                rewrite (.*) /index.php last;
                }
                }

  4. 坑爹网 : 2013年04月24日09:23:16  -46楼

    你好,如果是nginx的伪静态应该如何弄呢?

    • clairelume : 2013年04月24日15:42:44

      不用做设置就可以 😛
      if (!-e $request_filename)
      {
      rewrite (.*) /index.php last;
      }

      • 坑爹网 : 2013年04月25日08:22:06

        我用的是lnmp上默认的,也不用修改吗?

        location / {
        if (-f $request_filename/index.html){
        rewrite (.*) $1/index.html break;
        }
        if (-f $request_filename/index.php){
        rewrite (.*) $1/index.php;
        }
        if (!-f $request_filename){
        rewrite (.*) /index.php;
        }
        }

        • clairelume : 2013年04月26日18:01:29

          是的,不用修改就可以的

          • 坑爹网 : 2013年04月26日18:03:17

            🙂 非常感谢

  5. Geelg : 2013年06月19日17:28:28  -45楼

    我也遇到这样的问题
    http://www.geelg.com/creative/12328_3.html 点击之后就显示了下面的链入
    http://www.geelg.com/creative/12328.html/3