關于自定義文章類型和固定鏈接結(jié)構(gòu),大家可以想回顧一下:
- WordPress 自定義文章類型 介紹及實例解說(上)
- WordPress 自定義文章類型 介紹及實例解說(下)
- WordPress快速添加多個自定義文章類型
- WordPress自定義文章類型的固定鏈接設置插件:Custom Post Type Permalinks
自定義文章類型默認輸入的固定鏈接結(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 反饋及修復評論分頁問題。





現(xiàn)在4.9的已經(jīng)有自定義功能了,直接設置一下自定義就可以了
做的企業(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不知道如何是好啊~求助
試試
http://www.ydqwiac.cn/custom-post-type-permalinks.html
或
http://www.ydqwiac.cn/custom-post-type-permalink-code.html
謝謝倡萌回復。兩種都試過,but出現(xiàn)了404,我在網(wǎ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’
);
@倡萌 ,遇到一個問題請教下,我建立了一個自定義文章類型store,在固定鏈接這里出現(xiàn)了問題,如果固定鏈接設置成 /archives/%post_id% 的時候,store歸檔頁面變成了 /archives/store ,URL中多了一個archives,如果設置成其他的就會正常的是/store/ 這種格式。現(xiàn)在我想用/archives/%post_id%這種固定鏈接,要怎么將store歸檔頁面URL中的archives去掉呢?請指教!!!
沒深入研究過 這個問題,建議你試試 http://www.ydqwiac.cn/custom-post-type-permalinks.html 這個插件
使用這個插件并不能把archives去掉