用.html作为url后缀时的分页链接问题

固定链接设为/archives/%postname%.html时可以让页面看起来像静态页,同时会使分页链接变得十分奇怪,比如评论的分页链接会变成"hello-world.html/comment-page-1#comments",html既然是后缀就应该一直在最后,本文介绍如何实现。
目标
假设页面链接为hello-world.html

当在文章中插入分页时,希望分页链接格式为hello-world/page-2.html

评论分页链接则为hello-world/comment-page-2.html

实现方法
通过filter将分页链接改成希望的格式,分别用到wp_link_pages_link和get_comments_pagenum_link。
添加自定义跳转规则,利用filter rewrite_rules_array
取消Canonical URL(标准链接)跳转,否则使用新链接访问时WordPress会强制跳转到原来的链接
代码
下面这段代码放在主题的functions.php中,保存后需要到设置中重新保存一下固定链接。

如果你接受插件,也可下载插件版,激活即可。

修改文章/评论分页链接格式插件 已下载 653 次

本代码适用于固定链接格式为/archives/%postname%.html,若固定格式不同需要作相应修改,修改方法见下文。插件版也一样要改。

若固定链接格式为/%postname%.html,请修改规则,将

$this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';

改为

$this->post_rule = '([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
$this->comment_rule = '([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';

实现代码

class Rewrite_Inner_Page_Links{
 
 var $separator;
 
 var $post_rule;
 
 var $comment_rule;
 
 function __construct(){
 
 $this->separator = '/page-';
 
 $this->post_rule = 'archives/([^/]+)('.$this->separator.'([0-9]+))?.html/?$';
 
 $this->comment_rule = 'archives/([^/]+)/comment-page-([0-9]{1,}).html(\#[^\s])?$';
 
 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' ) );
 endif;
 }
 
 /**
 * 修改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->post_rule ] = 'index.php?name=$matches[1]&page=$matches[3]';
 $new_rule[ $this->comment_rule ] = '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();

本文介绍的方法演示了修改固定链接格式、并添加新Rewrite rules的方法,适用于其他情况。例如修改custom post type的固定链接,不同的是用哪个filter来修改链接输出格式。

阅读全文
 收藏 (1) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:轻语博客 » 用.html作为url后缀时的分页链接问题
分享到: 生成海报
qux主题真好用,功能强大,界面美观,还一直在更新.....

热门推荐

评论 1

评论前必须登录!

立即登录   注册

  1. #1

    不错,非常不错

    guangge6年前 (2017-12-23)国内网友谷歌浏览器 Windows 7 登录以回复 举报评论

本站承接WordPress主题开发,主题定制,功能开发

QQ咨询轻语云
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码