Wordpress 分页文章静态化的更优解决方案

Posted by David on 2008-11-12 in Blog Related

之前用比较暴力的方式实现了分页文章的静态化,不过这样一来升级 Wordpress 就不太方便了。厌烦了每次升级都要修改源文件,于是利用 Wordpress 本身提供的接口实现了更好的解决方案。

/%year%/%monthnum%/%postname%.html这样的永久链接结构为例:

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

// 添加分页处理规则
function add_custom_post_rewrite_rules($rules) {
  $custom_rules = array(
    '([0-9]{4})/([0-9]{1,2})/([^/]+)-([0-9]+)\.html$' => 'index.php?year=$matches[1]&monthnum=$matches[2]&name=$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('|([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. 后台“设置-永久链接”点击一下“保存修改”按钮,大功告成。

至于 life97 同学说的 .html/xxx 形式的链接访问失效,这个可能和服务器设置有关,具体原因我也不是很清楚,不过可以通过在 .htaccess 文件中添加规则来解决此问题,如:

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

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

下面是几种其它永久链接结构的具体修改代码:

UPDATE at 2009-4-1
添加/%year%/%monthnum%%day%/%post_id%.html/html/%post_id%.html这两种永久链接结构的修改代码

UPDATE at 2009-4-25
添加/%category%-%post_id%.html永久链接结构的修改代码

UPDATE at 2009-5-31
添加/%category%/%postname%.html永久链接结构的修改代码

UPDATE at 2009-9-30
添加/%category%/%year%/%monthnum%/%day%/%post_id%.html永久链接结构的修改代码

Tags: , , , , .

Comments

  1. 1 alfiereply to this comment

    hello,

    请教一下,你的archive是怎么做的?方便分享一下你的archive.php吗?我很喜欢这种风格。

  2. 2 Davidreply to this comment
  3. 3 life97reply to this comment

    :lol: 好人哪,解燃眉之急,雪中送炭。而且还特意为我的永久链接格式而准备好只需下载就行,真的激动得无言表达。只能还是用这么简单的一句:谢谢。

  4. 4 Davidreply to this comment

    @life97:
    举手之劳而已 :-)

  5. 5 life97reply to this comment

    这个my_wp_link_pages,有没有办法像原来的wp_link_pages那样,可以自行定义样式?比如:my_wp_link_pages('before=&after=&next_or_number=text&previouspagelink=上一頁&nextpagelink=下一頁')

  6. 6 Davidreply to this comment

    @life97: 可以的

  7. 7 kjkjkljkjreply to this comment

    请问这种方法只对%postname%.html有效吗?能不能提供/%year%%monthnum%/%post_id%.html这样的永久链接的解决方法?

  8. 8 DigCarsreply to this comment

    问题同上。

  9. 9 DigCarsreply to this comment

    post_id.html链接方法。如下:

    ..article/post_id.html
    关键的两行代码是:
    'article/([^/]+)-([0-9]+).html$' => 'index.php?p=$matches[1]&page=$matches[2]',
    );

    第一行按照默认的。index.php访问文章的参数设置

    $links = preg_replace_callback('|(article/)([^/]+)(.html)(/)([0-9]+)|', 'custom_page_link', $links);

    这个是你想要的链接的样式。。。。

    // 添加分页处理规则
    function add_custom_post_rewrite_rules($rules) {
    $custom_rules = array(
    'article/([^/]+)-([0-9]+).html$' => 'index.php?p=$matches[1]&page=$matches[2]',
    );
    $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('|(article/)([^/]+)(.html)(/)([0-9]+)|', 'custom_page_link', $links);

    echo $links;
    }

    function custom_page_link($matches) {
    return $matches[1].$matches[2].'-'.$matches[5].$matches[3];
    }
    /*文章分页 链接优化 结束*/

  10. 10 Davidreply to this comment

    @kjkjkljkj:
    @DigCars:

    已提供。

  11. 11 chenreply to this comment

    我使用的永久链接结构是/%category%-%post_id%.html,请问怎样相对应的修改代码呢?期盼博主的回复.

  12. 12 Davidreply to this comment
  13. 13 chenreply to this comment

    谢谢博主的回复!我修改代码后,重新保存过永久链接方式,也根据博主的重写规则重写了.htaccess,这样修改好了链接的结构,但点开分页出现了404错误,请问是什么原因呢?希望能指点一下。页面如下:http://www.lunweninfo.com/yiyao/yixue-333.html,点分页就出现404错误.

  14. 14 Davidreply to this comment

    @chen: 是我没有考虑到多层目录的情况,已经把代码更新了,再试试。

  15. 15 chenreply to this comment

    谢谢博主!已经修改成功了。还请问博主两个问题:一是怎样把分页显示到中间呢?现在显示是在左下角。二是,有办法实现分章自动分页吗?因为我文章一般比较长,经常需要分页,但在网上搜了很久没搜到相关办法或者插件.

  16. 16 chenreply to this comment

    博主,刚发现刚才修改链接结构的问题,就是一篇文章有三分页,打开第二分页,没有显示修改成功后的效果,但打开第三页后,再打开第二页,就有显示修改成功后的效果,很奇怪。页面如下:http://www.lunweninfo.com/yiyao/yixue-333.html

  17. 17 Davidreply to this comment

    @chen: 一个bug,已经修复,再试试

    居中显示问题, 请参考这样调用分页显示代码 my_wp_link_pages('before=<div style="text-align:center"><p style="display:inline"><strong>页面</strong>&after=</p></div>')

  18. 18 chenreply to this comment

    非常感谢博主.

  19. 19 lucktureply to this comment

    不好意思,学习研究了一番,自己不懂程序,还是没有成功,只好请博主帮忙了:
    请问 /html/%year%/%monthnum%/%post_id%.html 该怎么写,谢谢!

  20. 20 lucktureply to this comment

    如果能修改成与前面没有关系,只改分页的部分就好了,这样固定链接可以随便改而不再需要修改这里的代码! :lol:

  21. 21 david1900reply to this comment

    “ David”站长你好!
    我的wordpress是用的“/%category%/%postname%.html”固定连接,但是我更改后仍然是"******.html/2"的形式,我不知道怎么写,都弄了两天了,请指导,谢谢!

  22. 22 lucktureply to this comment

    博主:我在本地测试“/%category%-%post_id%.html”,出现这样的错误是什么原因:
    Fatal error: Call to undefined function my_wp_link_pages() in E:\NET\test\www\htdocs\wp\wp-content\themes\elegant-box\single.php on line 35
    谢谢!!

  23. 23 lucktureply to this comment

    谢谢博主的方法,虽然没有等到你的解答,但是还是利用你的方法,反复试验终于成功了。
    原来是添加那段定义代码的位置错了,所以没效果。
    应该添加到functions.php的最后“?>”之前。

  24. 24 mikereply to this comment

    你好,请问下我的主题没有single post。php 怎办?

  25. 25 Davidreply to this comment

    @mike: 找到调用 wp_link_pages 函数的地方,修改成 my_wp_link_pages 即可

  26. 26 Davidreply to this comment

    @lucktu: 前两天比较忙,没空上来看,能自己解决最好 ^-^

  27. 27 Davidreply to this comment
  28. 28 david1900reply to this comment

    谢谢David站长解答!

  29. 29 wellreply to this comment

    我怎么还是弄不好啊,你可以加我qq吗?谢谢啦,我真的没办法啦,求求你帮帮我啊

  30. 30 Davidreply to this comment

    @well: 你是 ???

  31. 31 wellreply to this comment

    287075449我的qq,,希望你能加我,到现在我的问题还没解决

  32. 32 wellreply to this comment

    我用的是cos-html-cache 1.1版本的,高版本的我用不了。高版本的不自动生成HTML页面。
    我按照你的方法,也行不通。后来我用了PGAENAVI插件进行分页,还是一样的分页面不自动生成HTML,所以报错404
    我的留言也出问题了,点击提交留言,就会报错404,但实际留言成功了。

  33. 33 Davidreply to this comment

    @well: 我想你可能是误解了,我提供的方法是配合 cos-html-cache 解决分页文章只能生成第一页html的问题,与cos-html-cache高低版本无关。另外,据我说知PGAENAVI不是用来对文章进行分页的吧?

  34. 34 Fansreply to this comment

    求教博主 “/%category%/%year%/%monthnum%/%day%/%post_id%.htm“这个URL格式怎么写REWRITE规则?我想把文章分页地址重写成“http://www.XXX.com/linux/2009/09/28/13239_2.htm“这种形式,想直接用htaccess实现,我该如何些规则?

  35. 35 Davidreply to this comment
  36. 36 Fansreply to this comment

    万分感谢!! :mrgreen:

  37. 37 chrisreply to this comment

    博主您好,我使用的永久链接结构是/%category%/%post_id%.html,请问怎样相对应的修改代码呢?期盼博主的回复.

  38. 38 Davidreply to this comment
  39. 39 pzpcreply to this comment

    刚安装了WP,找到楼主这篇文章,但是没看懂.想问下 /html/%postname%.html 这样的怎么修改?谢谢.

  40. 40 Davidreply to this comment
  41. 41 pzpcreply to this comment

    好像还是没有搞定,我的博客是在子目录下面安装的.地址是www.xxxx.com/xxxx/post/%postname%.html这样的,
    DirectAdmin管理面板,没有安装插件,把上面/html/改成/post/或/xxxx/post/,都没看到博客有什么变化啊?
    是在.htaccess文件中添加,.htaccess文件在博客安装目录下面,可是点"保存修改"按钮,修改的部分就没有了啊?麻烦楼主了.

  42. 42 Davidreply to this comment

    @pzpc:
    1) /html/%postname%.html 和 http://www.xxxx.com/xxxx/post/%postname%.html 完全是两个概念,请告诉我你的博客地址
    2) 在 .htaccess 文件中添加规则必须是在第三步之后

  43. 43 pzpcreply to this comment

    汗,不好意思,是我搞错了,你们讨论的/html是静态化,而我说的是伪静态?我是在本地测试的,我的博客地址是www.iampzpc.com/blog,/post/%postname%.html是直接在固定链接那里修改的,没用任何插件,麻烦楼主帮忙看看.刚换的程序,因为是个人日记,1个月1贴,5000-10000字,每天修改,所以想分页但不想静态化.

  44. 44 Davidreply to this comment

    @pzpc: 如果你只是想分页的话,在后台编辑日志内容的时候在合适的地方插入 [!--nextpage--] 标签(请把[]替换成尖括号)就可以了

  45. 45 pzpcreply to this comment

    谢谢你告诉我这个标签,但是我想问的是像你这样的%postname%-x.html格式,^_^

  46. 46 Davidreply to this comment

    @pzpc: see http://img.voidman.com/wp/2008/11/post-postname.txt
    因为不是静态化页面,所以不存在“.html/xxx 形式的链接访问失效”的问题,就不用手动修改 .htaccess 文件了

Leave a Reply

: when reply to my comment.

;-) :| :x :twisted: :smile: :shock: :sad: :roll: :razz: :oops: :o :mrgreen: :lol: :idea: :grin: :evil: :cry: :cool: :arrow: :???: :?: :!:

You can use these XHTML tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">