我们通常将WordPress固定链接设为/%postname%.html或者/%post_id%.html,可以让页面看起来像静态页,但当文章有分页时,分页链接会变得奇怪,比如:
/morning-paper-news.html/3
/132.html/2
html既然是后缀就应该一直在最后,来自solagirl的《用.html作为url后缀时的分页链接问题》一文,为我们提供了解决办法。
不过原代码只提供了/%postname%.html的修改方法。
本文提供一下/%post_id%.html的修改方法。
将下面代码添加到当前主题 functions.php中:
// 适合/%post_id%.html分页链接修正 class Rewrite_Inner_Page_Links_id{ var $separator; function __construct(){ $this->separator = '/page-'; if( !is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } // 修改post分页链接的格式 function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator."$2$1", $link ); } } return $link; } // 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录 function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([0-9]+).html(/[0-9]+)?/?$' ) { unset($rules[$rule]); } } $new_rule['([0-9]+)('.$this->separator.'([0-9]+))?.html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]'; return $new_rule + $rules; } // 禁止WordPress将页面分页链接跳转到原来的格式 function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links_id();
提示:添加代码后,需要保存一下固定链接设置。
之后再次打开文章分页链接,会变成类似的:
/morning-paper-news/page-2.html
/132/page-2.html
注:上述代码并没有评论分页的链接修正,本人无此刚需未做研究。
应网友要求补一个:/%category%/%post_id%.html格式的
貌似这种格式也经常有人问,这里就提供一下修改方法:
将上述代码中第36行:
$new_rule['([0-9]+)('.$this->separator.'([0-9]+))?.html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';
改为:
$new_rule['[^/]+/([0-9]+)('.$this->separator.'([0-9]+))?.html/?$'] = 'index.php?p=$matches[1]&page=$matches[3]';
只是加了个[^/]+/
最后分页形式是:
分类名称/132/page-2.html
添加修改上述代码后,不要忘记到后台保存一下固定链接设置,否则不会生效!
其它固定链接形式,需要安装rewrite rules inspector插件查看链接正则写法并修改上述代码。
有主题用户问,为什么不集成到主题中?原因很简单,大家的固定链接形式各不相同,这段代码没有通用性,而且还是有很多BUG,如自定义分类法文章就无法使用,添加上述代码后,可能文章都无法打开...
附:适合/%postname%.html链接形式原代码:
// 修复.html分页链接 class Rewrite_Inner_Page_Links{ var $separator; function __construct(){ $this->separator = '/page-'; if( !is_admin() || defined( 'DOING_AJAX' ) ) : add_filter( 'wp_link_pages_link', array( $this, 'inner_page_link_format' ), 10, 2 ); // for inner pages add_filter( 'get_comments_pagenum_link', array( $this, 'comment_page_link_format' ) ); add_filter( 'redirect_canonical', array( $this, 'cancel_redirect_for_paged_posts' ), 10, 2 ); endif; if( is_admin() ) : add_filter( 'rewrite_rules_array', array( $this, 'pagelink_rewrite_rules' ) ); register_activation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ) ; register_deactivation_hook( __FILE__, array( $this, 'flush_rewrite_rules' ) ); endif; } function flush_rewrite_rules(){ flush_rewrite_rules(); } /** * 修改post分页链接的格式 * @param string $link * @param int $number * @return string */ function inner_page_link_format( $link, $number ){ if( $number > 1 ){ if( preg_match( '%<a href=".*\.html/\d*"%', $link ) ){ $link = preg_replace( "%(\.html)/(\d*)%", $this->separator."$2$1", $link ); } } return $link; } /** * 修改评论分页链接 * @param string $result * @return string */ function comment_page_link_format( $result ){ // From hello-world.html/comment-page-1#comments to hello-world/comment-page-1.html#comments if( strpos( $result, '.html/' ) !== false ){ $result = preg_replace( '=([^/]+)(.html)/comment-page-([0-9]{1,})=', "$1/comment-page-$3$2" ,$result ); } return $result; } /** * 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录 * * 访问原始链接将返回404 * @param array $rules * @return array */ function pagelink_rewrite_rules( $rules ){ foreach ($rules as $rule => $rewrite) { if ( $rule == '([^/]+).html(/[0-9]+)?/?$' || $rule == '([^/]+).html/comment-page-([0-9]{1,})/?$' ) { unset($rules[$rule]); } } $new_rule['([^/]+)('.$this->separator.'([0-9]+))?.html/?$'] = 'index.php?name=$matches[1]&page=$matches[3]'; $new_rule['([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$'] = 'index.php?name=$matches[1]&cpage=$matches[2]'; return $new_rule + $rules; } /** * 禁止WordPress将页面分页链接跳转到原来的格式 * @param string $redirect_url * @param string $requested_url * @return bool */ function cancel_redirect_for_paged_posts( $redirect_url, $requested_url ){ global $wp_query; if( is_single() && $wp_query->get( 'page' ) > 1 ){ return false; } return true; } } new Rewrite_Inner_Page_Links();
上述代码会可能会与No category parents插件冲突,解决办法:
打开No category parents插件no-category-parents.php文件,在大约54行找到:
- //if ( ! isset( $rules['(.+?)-cat/?$'] ) ) { // have to comment this in order to refresh the rules
- global $wp_rewrite;
- $wp_rewrite->flush_rules();
- //}
去掉注释,改为:
- if ( ! isset( $rules['(.+?)-cat/?$'] ) ) { // have to comment this in order to refresh the rules
- global $wp_rewrite;
- $wp_rewrite->flush_rules();
- }
不过还真不知道这句默认注释掉的用处,经测试未发现有什么问题。
如果不想用rewrite rules inspector插件查看链接规则,可以将下面代码添加到主题functions.php中,之后打开固定链接设置页面会看到链接规则。
add_filter( 'rewrite_rules_array', 'show_rewrite_rules' ); function show_rewrite_rules( $rules ) { echo nl2br( var_export( $rules, true ) ); die; }
本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!

101F
// 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
这个功能无效 还是301了原来的 /132.html/2 这种链接
102F
分页后404咋办。。。
B1
@ 台庄资讯 添加完代码,保存一下固定链接设置
B2
@ 知更鸟 保存固定连接还是404,我的服务器是nginx
103F
过来学习一下下
104F
学习一下
105F
感谢分享,解决了我好多年前就纠结的问题
106F
看看是不是我想要的结果,感谢~~~
107F
感谢分享!!!
108F
看看隐藏内容啊
109F
正需要看一下
110F
谢谢分享
111F
/%category%/%year%/%second%%post_id%.html
应该如何修改?
112F
学习了,谢谢
113F
来学习下
114F
网站不错呀反应太别快,支持一下
115F
设置分页后,访问分页内容,统计代码不统计PV数量。
116F
学习下
117F
学习下
118F
学习了
119F
谢谢分享
120F
感谢分享
121F
感谢分享
122F
看看怎么解决插件冲突的
123F
学习高手
124F
我来学习下!
125F
好不容易搞定分页,结果却出现/132.html/2,这篇文章真的帮了大忙了。
126F
请教,/%category%/%post_id%.html
我这个多了一层。
B1
@ Stone.Sam 修改方法已加文章中
B2
@ 知更鸟 很感谢您啊,第一次用WP,要学习的地方还很多。
B2
@ 知更鸟 这个代码好像和Super Static Cache有冲突,我使用了这个代码,所有页面全部跳转首页。
127F
评论分页有问题,无法查看前面的评论
128F
折腾了一下, 评论分页就404
B1
@ 两天 这玩意要是好用的话,我早就加主题中了
我试过,不是404,只是没改链接形式而已
B2
@ 知更鸟 我看着除了评论分页,其他分页还可以
B3
@ 两天 原代码,评论也不能实现他说的功能