問題描述
我正在嘗試建立一個 multi-level 自定義後置型別結構,其固定連結看起來像 authors/books/chapters,作者,書籍和章節都設定為自己的自定義帖子型別。例如,此網站上的典型網址可能如 example.com/authors/stephen-king/the-shining/chapter-3/
每章只能屬於一本書,每本書只能屬於一位作者。我考慮使用分類法而不是 CPT 作為作者和書籍,但是我需要將後設資料與每個專案相關聯,我更喜歡這個帖子介面。
我最喜歡的方式是簡單地將每個自定義帖子設定為 CPT 一個級別的條目的孩子。例如,我建立 「第 3 章」,並使用自定義 meta-box 將”The Shining” 分配為父級。 “The Shining” 依次將”Stephen King” 作為母公司。創造這些關係我沒有任何麻煩。
我在 CPT s’m 中使用重寫標籤,並且固定連結想要工作,但是它們並不完全正確。使用 re-write 分析器,我可以看到重寫規則實際上是生成的,但是它們似乎沒有正確的順序,因此先處理其他規則。
以下是我註冊了 CPT 的方式:
function cpt_init() {
$labels = array(
'name' => 'Authors'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'author',
'with_front' => FALSE,
),
'with_front' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor' )
);
register_post_type('authors',$args);
$labels = array(
'name' => 'Books'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'author/%authors%',
'with_front' => FALSE,
),
'with_front' => false,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor' )
);
register_post_type('books',$args);
$labels = array(
'name' => 'Chapters'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array(
'slug' => 'author/%authors%/%books%',
'with_front' => FALSE,
),
'with_front' => FALSE,
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => true,
'menu_position' => null,
'supports' => array( 'title', 'editor' )
);
register_post_type('chapters',$args);
}
add_action( 'init', 'cpt_init' );
那麼有什麼辦法可以改變我的重寫規則的優先順序,以便作者,書籍和章節都首先匹配?
我也知道我將要新增一個 post_type_link 過濾器,但這似乎是次要的獲得固定連結在第一位。如果有人知道在哪裡可以找到關於該過濾器如何工作的全面概述,那將不勝感激。
最佳解決方案
如果你想保持’ 作者’ 為固定連結的基礎,例如 example.com/authors/stephen-king/為’ 作者’CPT,example.com/authors/stephen-king/the-shining/為’ 書’CPT 和例子.com /authors /stephen-king /the-shining /chapter-3 /對於 「章節」CPT,WordPress 會認為幾乎所有的都是 「作者」 帖子或 「作者」 帖子的等級小孩,因為不是這樣 WordPress 最終變得非常困惑。
就這樣說,有一個相當基本的解決方法,但是隻要你的永久連結結構總是遵循相同的順序,即’authors’ 這個詞總是跟著一個作者的 s,,後面是一個書呆子,它總是跟著一個一章 s lug,那麼你應該很好去。
在此解決方案中,無需在’chapters’ 和’books’ 的自定義後期型別定義中定義重寫段,但將’authors’ 重寫塊作為簡單的’authors’ 設定,將以下程式碼放在您的 functions.php 檔案和”flush” 中您的重寫規則。
add_action( 'init', 'my_website_add_rewrite_tag' );
function my_website_add_rewrite_tag() {
// defines the rewrite structure for 'chapters', needs to go first because the structure is longer
// says that if the URL matches this rule, then it should display the 'chapters' post whose post name matches the last slug set
add_rewrite_rule( '^authors/([^/]*)/([^/]*)/([^/]*)/?','index.php?chapters=$matches[3]','top' );
// defines the rewrite structure for 'books'
// says that if the URL matches this rule, then it should display the 'books' post whose post name matches the last slug set
add_rewrite_rule( '^authors/([^/]*)/([^/]*)/?','index.php?books=$matches[2]','top' );
}
// this filter runs whenever WordPress requests a post permalink, i.e. get_permalink(), etc.
// we will return our custom permalink for 'books' and 'chapters'. 'authors' is already good to go since we defined its rewrite slug in the CPT definition.
add_filter( 'post_type_link', 'my_website_filter_post_type_link', 1, 4 );
function my_website_filter_post_type_link( $post_link, $post, $leavename, $sample ) {
switch( $post->post_type ) {
case 'books':
// I spoke with Dalton and he is using the CPT-onomies plugin to relate his custom post types so for this example, we are retrieving CPT-onomy information. this code can obviously be tweaked with whatever it takes to retrieve the desired information.
// we need to find the author the book belongs to. using array_shift() makes sure only one author is allowed
if ( $author = array_shift( wp_get_object_terms( $post->ID, 'authors' ) ) ) {
if ( isset( $author->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( 'authors/' . $author->slug . '/' . $post->post_name ) );
}
}
break;
case 'chapters':
// I spoke with Dalton and he is using the CPT-onomies plugin to relate his custom post types so for this example, we are retrieving CPT-onomy information. this code can obviously be tweaked with whatever it takes to retrieve the desired information.
// we need to find the book it belongs to. using array_shift() makes sure only one book is allowed
if ( $book = array_shift( wp_get_object_terms( $post->ID, 'books' ) ) ) {
// now to find the author the book belongs to. using array_shift() makes sure only one author is allowed
$author = array_shift( wp_get_object_terms( $book->term_id, 'authors' ) );
if ( isset( $book->slug ) && $author && isset( $author->slug ) ) {
// create the new permalink
$post_link = home_url( user_trailingslashit( 'authors/' . $author->slug . '/' . $book->slug . '/' . $post->post_name ) );
}
}
break;
}
return $post_link;
}
Learn more about the CPT-onomies plugin
次佳解決方案
我沒有這樣的場景的個人經驗,但 Randy Hoyt 上週末在 WordCamp San Fran 做了一個演講,就像你在說的 「下屬型別」 一樣。
這是他的演講頁面,其中包括他的演示幻燈片和連結到一個外掛,他為下屬的職位型別工作:http://randyhoyt.com/wordpress/subordinate-post-types/
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。
