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 文件了

  47. 47 Carriereply to this comment

    您好,我也想問的是像你這樣的%postname%-x.html格式,因為最近才改用這個緩存插件,有很多不懂的地方。

  48. 48 Davidreply to this comment

    @Carrie: 其实如果不是为了真正静态化,没太多必要这么操作,%postname% 的形式也挺好

  49. 49 Carriereply to this comment

    您好,我的意思是,你提供的改法,我也可以用嗎?我該用上面那些檔案裏的哪一個呢!?我目前的部落格已經用 cos 緩存插件了,網址型態也都改了,就是用 /post/%postname%.html 的結構,所以我的分頁文章應該就是要改成 %postname%-x.html 這樣吧!?還是維持原來的就可以呢!?

  50. 50 Davidreply to this comment

    @Carrie: 抱歉现在才看见你的留言。
    根据你采用的 /post/%postname%.html 的URL结构,要做的修改请参看 http://img.voidman.com/wp/2008/11/post-postname.txt

  51. 51 Carriereply to this comment

    @David: 感謝您的回覆,剛巧我最近新寫了一款主題可以運用上去,多謝喔!

  52. 52 neekereply to this comment

    嗯,不错不错,自力更生。 ;-)

  53. 53 Carriereply to this comment

    @David: 用了你提供的方法,結果會造成 IE 核心建構的瀏覽器白屏,其它瀏覽器倒是沒這個問題,雖然我也討厭 IE,不過目前主流瀏覽器 IE 還是佔絕大多數的使用者,所以就把你的方法從主題函數表檔案裏刪除了。

  54. 54 Davidreply to this comment

    @Carrie: 从您的描述中我不太好判断是什么原因引起白屏现象,可以开启wordpress的调试模式,看看是否有错误信息输出。

  55. 55 Carriereply to this comment

    @David: 具體的原因我真的不清楚,把主題所有的檔案全部找過了,唯一的問題就是您提供的這個函數最有可能造成 IE 白屏現象,去掉了函數之後,IE(包含其他使用 IE 核心建構的瀏覽器)就能恢復正常瀏覽了。

  56. 56 panpanreply to this comment

    你好,用了你的分页静态化,我这里调用就出错了。想麻烦你帮忙看下什么原因
    我的Q:29868582

  57. 57 Davidreply to this comment

    @Carrie: 我测试了 /post/%postname%.html 对应的修改代码,没有发现你说的白屏问题,可能函数名称与你的wp文件里的产生冲突了,猜测。另外,之前提供的 /post/%postname%.html 的修改代码里有处错误,index.php?p= 应该是 index.php?name= ,不过这个应该是造成404错误而不是白屏。

  58. 58 Davidreply to this comment

    @panpan: 请说明你用的是哪个修改代码,以及出错信息。

  59. 59 Carriereply to this comment

    @David: 沒關係了,我想應該是這個快取插件的原因,我現在已經轉用 Hyper Cache 這個緩存插件了。不過還是要多謝你的回覆 :)

  60. 60 panpanreply to this comment

    /archives/%post_id%.html这种形式,反复测试,改了之后怎么没反映?还是以 xx.html/x 形式打开。

  61. 61 随缘reply to this comment

    真不错,谢谢分享 :o
    主题也很不错

  62. 62 Davidreply to this comment

    @panpan: 按照你提供的永久链接格式,需要使用该修改代码 http://img.voidman.com/wp/2008/11/archives-post_id.txt

  63. 63 暮色森林reply to this comment

    博主你好,我想请教下如果是 http://www.xxx.com/postname.html 这种直接根目录接文章名该如何写,上面的几种都试过了,都不成功

  64. 64 Davidreply to this comment
  65. 65 暮色森林reply to this comment

    @David:
    谢谢博主,搞定了,不过就是有点不美观,有办法控制吗 :mrgreen:

  66. 66 Davidreply to this comment

    @暮色森林: 不知道你指的是什么不美观?

  67. 67 暮色森林reply to this comment

    @David: 就是如果分页比较多的话会从一开始到末尾全显示出来,能不能改成“首页 1 2 3 …… n-1 n 末尾”这样的形式呢?只在一排显示,中间部分用……代替

  68. 68 暮色森林reply to this comment

    话说你回复的好快喔 :lol:

  69. 69 Davidreply to this comment

    @暮色森林: 分页的链接是调用wp自带的函数生成的,貌似没有提供这个选项。ps,一篇文章也没必要分太多页吧?

  70. 70 暮色森林reply to this comment

    @David: 呃,转载了篇小说,所以太多了,以后再也不转了,直接嵌套文库,还好看!

Trackbacks / Pingbacks

  1. » 2.9.2手动长篇文章分页插件(无需安装任何插件) | 也许忘记'Blog
  2. » WordPress文章分页方法,附静态化分页链接美化 | 游子网络
  3. » Wordpress 页面静态化后无法打开分页的解决方法 - FORECE's 博客日志

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="">