當(dāng)前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress 修改自定義文章類型的固定鏈接結(jié)構(gòu)

WordPress 修改自定義文章類型的固定鏈接結(jié)構(gòu)

關(guān)于自定義文章類型固定鏈接結(jié)構(gòu),大家可以想回顧一下:

自定義文章類型默認(rèn)輸入的固定鏈接結(jié)構(gòu)為 /%postname%  。假設(shè)我們添加的自定義文章類型為 book ,那么默認(rèn)輸出的 book 文章鏈接一般為 http://域名/book/slug (slug為標(biāo)題別名)。如果文章標(biāo)題是中文(比如:一本好書),而且你沒有手動(dòng)或者使用插件翻譯為非中文的 slug (a-nice-book),那么顯示的鏈接就會(huì)是 http://域名/book/一本好書 ,這樣一來,文章鏈接的中文部分就會(huì)顯示成亂碼,實(shí)在不符合我們的審美標(biāo)準(zhǔn)了。

那么,我們可以將 /%postname% 改為 /%post_id% /%post_id%.html 樣式,使用ID來顯示。要實(shí)現(xiàn)這個(gè)目的,可以使用文章開頭提到的 Custom Post Type Permalinks 插件。如果你是插件或主題開發(fā)者,一般都喜歡直接通過代碼定義好默認(rèn)的固定鏈接結(jié)構(gòu)。

可以在插件函數(shù)文件或主題的functions.php 文件添加下面的代碼:

/**
 * 設(shè)置 book 這種自定義文章類型的固定鏈接結(jié)構(gòu)為 ID.html 
 * http://www.ydqwiac.cn/custom-post-type-permalink-code.html
 */
add_filter('post_type_link', 'custom_book_link', 1, 3);
function custom_book_link( $link, $post = 0 ){
    if ( $post->post_type == 'book' ){
        return home_url( 'book/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'custom_book_rewrites_init' );
function custom_book_rewrites_init(){
    add_rewrite_rule(
        'book/([0-9]+)?.html$',
        'index.php?post_type=book&p=$matches[1]',
        'top' );
    add_rewrite_rule(
        'book/([0-9]+)?.html/comment-page-([0-9]{1,})$',
        'index.php?post_type=book&p=$matches[1]&cpage=$matches[2]',
        'top'
        );
}

以上代碼就可以輸出形如 /book/123.html 的鏈接。請(qǐng)將代碼中所有 book 替換為你的自定義文章類型。

如果你要同時(shí)定義多種自定義文章類型,可以使用下面的代碼:

/**
 * 設(shè)置多種自定義文章類型的固定鏈接結(jié)構(gòu)為 ID.html
 * http://www.ydqwiac.cn/custom-post-type-permalink-code.html
 */
$mytypes = array(//根據(jù)需要添加你的自定義文章類型
    'type1' => 'slug1',
    'type2' => 'slug2',
    'type3' => 'slug3'
    );
add_filter('post_type_link', 'my_custom_post_type_link', 1, 3);
function my_custom_post_type_link( $link, $post = 0 ){
    global $mytypes;
    if ( in_array( $post->post_type,array_keys($mytypes) ) ){
        return home_url( $mytypes[$post->post_type].'/' . $post->ID .'.html' );
    } else {
        return $link;
    }
}
add_action( 'init', 'my_custom_post_type_rewrites_init' );
function my_custom_post_type_rewrites_init(){
    global $mytypes;
    foreach( $mytypes as $k => $v ) {
        add_rewrite_rule(
            $v.'/([0-9]+)?.html$',
            'index.php?post_type='.$k.'&p=$matches[1]',
            'top'
            );
        add_rewrite_rule(
            $v.'/([0-9]+)?.html/comment-page-([0-9]{1,})$',
            'index.php?post_type='.$k.'&p=$matches[1]&cpage=$matches[2]',
            'top'
            );
    }
}

參考資料:http://www.solagirl.net/custom-post-type-permalink.html,感謝 Hello World  反饋及修復(fù)評(píng)論分頁問題。

聲明:本站所有文章,如無特殊說明或標(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
WordPress開發(fā)

WordPress wp_nav_menu()菜單輸出菜單描述

2013-9-3 9:13:48

WordPress開發(fā)

為WordPress的自定義菜單鏈接添加欄目圖標(biāo)

2013-9-4 8:38:28

28 條回復(fù) A文章作者 M管理員
  1. 安智網(wǎng)絡(luò)

    現(xiàn)在4.9的已經(jīng)有自定義功能了,直接設(shè)置一下自定義就可以了

  2. Joanna Jin

    做的企業(yè)站,基于后臺(tái)需要,用Custom Post Type UI自定義了product文章分類,又自定義了taxonomy名為protype,通過加過濾器,實(shí)現(xiàn)了網(wǎng)址如下:
    分類:http://www.ytxinhai1.com/product/crusher
    產(chǎn)品:http://www.ytxinhai1.com/product/crusher/3
    但是想給產(chǎn)品加.html,變成http://www.ytxinhai1.com/product/crusher/3.html不知道如何是好啊~求助

  3. Hello World

    這樣寫會(huì)導(dǎo)致文章評(píng)論分頁失效。

    • Hello World

      請(qǐng)將上面的代碼修改為:(解決自定義類型文章評(píng)論無法翻頁問題。)

      add_rewrite_rule(
      ‘book/([0-9]+)?.html$’,
      ‘index.php?post_type=book&p=$matches[1]’,
      ‘top’
      );
      add_rewrite_rule(
      ‘book/([0-9]+)?.html/comment-page-([0-9]{1,})$’,
      ‘index.php?post_type=book&p=$matches[1]&cpage=$matches[2]’,
      ‘top’
      );

  4. kimsom

    @倡萌 ,遇到一個(gè)問題請(qǐng)教下,我建立了一個(gè)自定義文章類型store,在固定鏈接這里出現(xiàn)了問題,如果固定鏈接設(shè)置成 /archives/%post_id% 的時(shí)候,store歸檔頁面變成了 /archives/store ,URL中多了一個(gè)archives,如果設(shè)置成其他的就會(huì)正常的是/store/ 這種格式。現(xiàn)在我想用/archives/%post_id%這種固定鏈接,要怎么將store歸檔頁面URL中的archives去掉呢?請(qǐng)指教!!!

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

宁河县| 洮南市| 万宁市| 开江县| 陇川县| 临武县| 阿克苏市| 罗源县| 兴海县| 辽宁省| 会昌县| 红桥区| 张家口市| 铜梁县| 时尚| 长兴县| 醴陵市| 商丘市| 桑植县| 项城市| 南靖县| 睢宁县| 承德县| 高雄市| 秀山| 大荔县| 通江县| 洞口县| 景德镇市| 柳林县| 甘德县| 万源市| 法库县| 工布江达县| 石狮市| 靖远县| 宣汉县| 东宁县| 咸阳市| 阳新县| 介休市|