外贸独立站的底层设计密码WordPress 成品站模板
当前位置:首页>WordPress建站>网站SEO>WordPress 用.html作为url后缀时的分页链接问题

WordPress 用.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

实现方法

  1. 通过filter将分页链接改成希望的格式,分别用到vwp_link_pages_link get_comments_pagenum_link
  2. 添加自定义跳转规则,利用filter rewrite_rules_array
  3. 取消Canonical URL(标准链接)跳转,否则使用新链接访问时WordPress会强制跳转到原来的链接

代码

下面这段代码放在主题的functions.php中,保存后需要到设置中重新保存一下固定链接

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();

本代码适用于固定链接格式为/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])?$';

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

原文出自:http://www.solagirl.net/permalink-with-html-suffix.html,感谢原作者 Sola!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
网站SEO

禁止WordPress向站内链接发送PingBack引用通告

2014-11-10 10:34:41

网站SEO

WordPress 自动给文章的关键词添加链接 Keywords to Links Converter

2015-11-8 8:05:07

11 条回复 A文章作者 M管理员
  1. huacunbook

    经测试,文章分页没问题,但是评论分页重写的网址可以打开,原先的网址也打不开了。

    但是,网页底部的评论分页链接却还是原来的,没有被替换成新链接,是不是钩子不对呢?能否修正一下,太感谢了!

  2. 如果是设置的postid.html这样怎么办,谢谢!

  3. 原来是这个样子的

  4. 谢了!

  5. 寻梦

    post_id怎么改呢。。。。

  6. 寻梦

    哎 第二页就404了 ➡

  7. ❗ 对于一些主题好像不起作用吧

  8. 直接用插件就解决 了··

    • 有啥插件 :mrgreen:

  9. 本文挺好,为啥没人评论?

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索