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

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

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

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

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

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

/**
 * 設置 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 的鏈接。請將代碼中所有 book 替換為你的自定義文章類型。

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

/**
 * 設置多種自定義文章類型的固定鏈接結(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  反饋及修復評論分頁問題。

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

給TA打賞
共{{data.count}}人
人已打賞
歡迎關注WordPress大學公眾號 WPDAXUE
WordPress開發(fā)

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

2013-9-3 9:13:48

WordPress開發(fā)

為WordPress的自定義菜單鏈接添加欄目圖標

2013-9-4 8:38:28

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

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

  2. Joanna Jin

    做的企業(yè)站,基于后臺需要,用Custom Post Type UI自定義了product文章分類,又自定義了taxonomy名為protype,通過加過濾器,實現(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

    這樣寫會導致文章評論分頁失效。

    • Hello World

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

      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

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

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

利川市| 大港区| 延津县| 潮安县| 北票市| 榆林市| 泸西县| 安庆市| 高尔夫| 沁阳市| 商丘市| 辽源市| 丹寨县| 万载县| 保德县| 嘉禾县| 瑞金市| 云阳县| 平罗县| 平顶山市| 宜宾县| 门头沟区| 盐津县| 东乡族自治县| 万宁市| 巨野县| 阿鲁科尔沁旗| 华阴市| 宜阳县| 唐山市| 静海县| 巴楚县| 山阳县| 毕节市| 瓮安县| 竹溪县| 调兵山市| 高淳县| 石阡县| 平远县| 阳曲县|