當(dāng)前位置:首頁>WordPress建站>網(wǎng)站SEO>WordPress 用.html作為url后綴時(shí)的分頁鏈接問題

WordPress 用.html作為url后綴時(shí)的分頁鏈接問題

固定鏈接設(shè)為 /archives/%postname%.html 時(shí)可以讓頁面看起來像靜態(tài)頁,同時(shí)會(huì)使分頁鏈接變得十分奇怪,比如評(píng)論的分頁鏈接會(huì)變成”hello-world.html/comment-page-1#comments”,html既然是后綴就應(yīng)該一直在最后,本文介紹如何實(shí)現(xiàn)。

目標(biāo)

假設(shè)頁面鏈接為hello-world.html

當(dāng)在文章中插入分頁時(shí),希望分頁鏈接格式為 hello-world/page-2.html

評(píng)論分頁鏈接則為 hello-world/comment-page-2.html

實(shí)現(xiàn)方法

  1. 通過filter將分頁鏈接改成希望的格式,分別用到vwp_link_pages_link get_comments_pagenum_link
  2. 添加自定義跳轉(zhuǎn)規(guī)則,利用filter rewrite_rules_array
  3. 取消Canonical URL(標(biāo)準(zhǔn)鏈接)跳轉(zhuǎn),否則使用新鏈接訪問時(shí)WordPress會(huì)強(qiáng)制跳轉(zhuǎn)到原來的鏈接

代碼

下面這段代碼放在主題的functions.php中,保存后需要到設(shè)置中重新保存一下固定鏈接

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;
    }
    /**
     * 修改評(píng)論分頁鏈接
     * @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;
    }
    /**
     * 為新的鏈接格式增加重定向規(guī)則,移除原始分頁鏈接的重定向規(guī)則,防止重復(fù)收錄
     *
     * 訪問原始鏈接將返回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將頁面分頁鏈接跳轉(zhuǎn)到原來的格式
     * @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,若固定格式不同需要作相應(yīng)修改,修改方法見下文。

若固定鏈接格式為/%postname%.html,請(qǐng)修改規(guī)則,將

$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的固定鏈接,不同的是用哪個(gè)filter來修改鏈接輸出格式。

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

聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學(xué)公眾號(hào) WPDAXUE
網(wǎng)站SEO

禁止WordPress向站內(nèi)鏈接發(fā)送PingBack引用通告

2014-11-10 10:34:41

網(wǎng)站SEO

WordPress 自動(dòng)給文章的關(guān)鍵詞添加鏈接 Keywords to Links Converter

2015-11-8 8:05:07

11 條回復(fù) A文章作者 M管理員
  1. huacunbook

    經(jīng)測(cè)試,文章分頁沒問題,但是評(píng)論分頁重寫的網(wǎng)址可以打開,原先的網(wǎng)址也打不開了。

    但是,網(wǎng)頁底部的評(píng)論分頁鏈接卻還是原來的,沒有被替換成新鏈接,是不是鉤子不對(duì)呢?能否修正一下,太感謝了!

  2. 如果是設(shè)置的postid.html這樣怎么辦,謝謝!

  3. 原來是這個(gè)樣子的

  4. 謝了!

  5. 尋夢(mèng)

    post_id怎么改呢。。。。

  6. 尋夢(mèng)

    哎 第二頁就404了 ?

  7. ? 對(duì)于一些主題好像不起作用吧

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

    • 有啥插件 :mrgreen:

  9. 本文挺好,為啥沒人評(píng)論?

?
個(gè)人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

万山特区| 嫩江县| 潢川县| 台中县| 威海市| 西畴县| 延安市| 黎川县| 临湘市| 从化市| 拜泉县| 上犹县| 乌鲁木齐市| 商水县| 周至县| 古浪县| 涡阳县| 仁寿县| 屯门区| 平果县| 田东县| 武胜县| 安阳县| 慈溪市| 密云县| 鹤山市| 博野县| 颍上县| 德化县| 定远县| 黔西县| 姜堰市| 安龙县| 永平县| 修武县| 永泰县| 日喀则市| 吉隆县| 苗栗县| 长春市| 渭源县|