問題描述
我想創建一個與類別類似的自定義分類法,作為類別的行為與默認帖子 (基於/%category%/%postname%/permalink 結構),以便自定義帖子類型中的帖子是顯示為 www.example.com/custom-post-type/custom-taxonomy-name/post-name/post-name 另外,我希望類別元框僅在我們添加新的默認帖子時顯示,而不是在自定義帖子類型中添加新帖子,而自定義分類框僅顯示當我們在自定義帖子類型中添加新帖子時,而不是在添加新的默認帖子時。
最佳解決方案
首先,如果要僅顯示分類法,只能將自定義帖子類型顯示為自定義帖子類型,然後通過將自定義帖子類型名稱作為參數傳遞到 register_taxonomy,然後將分類法註冊到該自定義帖子類型。通過這樣做,分類法 metabox 只出現在自定義帖子類型上。如果您不想顯示類別 metabox 到自定義帖子類型,那麼在註冊自定義帖子類型時刪除術語類別作為參數,而是包含類似這樣的分類標題名稱’taxonomies’ => 陣列 (‘post_tag’,’your_taxonomy_name’) 。這裏是我如何實現的代碼。我已經在自定義帖子類型主題下注冊了一個自定義分類法,並使用 slug themes_categories
function themes_taxonomy() {
register_taxonomy(
'themes_categories', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
'themes', //post type name
array(
'hierarchical' => true,
'label' => 'Themes store', //Display name
'query_var' => true,
'rewrite' => array(
'slug' => 'themes', // This controls the base slug that will display before each term
'with_front' => false // Don't display the category base before
)
)
);
}
add_action( 'init', 'themes_taxonomy');
然後改變永久鏈接我創建了以下功能
function filter_post_type_link($link, $post)
{
if ($post->post_type != 'themes')
return $link;
if ($cats = get_the_terms($post->ID, 'themes_categories'))
$link = str_replace('%themes_categories%', array_pop($cats)->slug, $link);
return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);
然後我註冊了一個自定義的帖子類型與 slug 主題如下
//Registering Custom Post Type Themes
add_action( 'init', 'register_themepost', 20 );
function register_themepost() {
$labels = array(
'name' => _x( 'Themes', 'my_custom_post','custom' ),
'singular_name' => _x( 'Theme', 'my_custom_post', 'custom' ),
'add_new' => _x( 'Add New', 'my_custom_post', 'custom' ),
'add_new_item' => _x( 'Add New ThemePost', 'my_custom_post', 'custom' ),
'edit_item' => _x( 'Edit ThemePost', 'my_custom_post', 'custom' ),
'new_item' => _x( 'New ThemePost', 'my_custom_post', 'custom' ),
'view_item' => _x( 'View ThemePost', 'my_custom_post', 'custom' ),
'search_items' => _x( 'Search ThemePosts', 'my_custom_post', 'custom' ),
'not_found' => _x( 'No ThemePosts found', 'my_custom_post', 'custom' ),
'not_found_in_trash' => _x( 'No ThemePosts found in Trash', 'my_custom_post', 'custom' ),
'parent_item_colon' => _x( 'Parent ThemePost:', 'my_custom_post', 'custom' ),
'menu_name' => _x( 'Themes Posts', 'my_custom_post', 'custom' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Custom Theme Posts',
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'post-formats', 'custom-fields' ),
'taxonomies' => array( 'post_tag','themes_categories'),
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => get_stylesheet_directory_uri() . '/functions/panel/images/catchinternet-small.png',
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'themes/%themes_categories%','with_front' => FALSE),
'public' => true,
'has_archive' => 'themes',
'capability_type' => 'post'
);
register_post_type( 'themes', $args );//max 20 charachter cannot contain capital letters and spaces
}
註冊自定義帖子時,您需要記住的幾件事情。將 has_archive 參數更改為自定義帖子類型段號,另一個將重寫段名稱更改為’slug’ => 「custom_post_type_slug /%taxonomy_slug%
現在,當您在寫入帖子類型頁面中添加新的帖子類型時,您將看到 http://www.example.com/wordpress/themes/%themes_categories%/post-name/的固定鏈接。如果沒有選擇此帖子的自定義分類法,則固定鏈接將保留 http://www.example.com/wordpress/themes/%themes_categories%/post-name/,然後會顯示不良請求。為了糾正這個,我們在自定義分類法中創建一個默認的術語。 (與類別中未分類相同) 將其添加到 functions.php
function default_taxonomy_term( $post_id, $post ) {
if ( 'publish' === $post->post_status ) {
$defaults = array(
'themes_categories' => array( 'other'), //
);
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( (array) $taxonomies as $taxonomy ) {
$terms = wp_get_post_terms( $post_id, $taxonomy );
if ( empty( $terms ) && array_key_exists( $taxonomy, $defaults ) ) {
wp_set_object_terms( $post_id, $defaults[$taxonomy], $taxonomy );
}
}
}
}
add_action( 'save_post', 'default_taxonomy_term', 100, 2 );
現在當自定義分類空白時,permlaink 自動成為 http://www.example.com/wordpress/themes/other/post-name/。
最後不要忘記通過點擊管理部分中的永久鏈接設置保存更改來刷新重寫,否則將被重定向到 404 錯誤。希望這可以幫助你。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。