問題描述
我有一個名為 portfolio 的自定義帖子類型,我試圖添加標籤分類法,我該怎麼做?
最佳解決方案
像這樣:(“portfolio” 在哪裏你將分類法註冊到一個帖子類型
add_action( 'init', 'create_tag_taxonomies', 0 );
//create two taxonomies, genres and tags for the post type "tag"
function create_tag_taxonomies()
{
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'separate_items_with_commas' => __( 'Separate tags with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
'menu_name' => __( 'Tags' ),
);
register_taxonomy('tag','portfolio',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'tag' ),
));
}
?>
次佳解決方案
或者只是添加:
'taxonomies' => array('post_tag')
An array of registered taxonomies like
categoryorpost_tagthat will be used with this post type. This can be used in lieu of callingregister_taxonomy_for_object_type()directly. Custom taxonomies still need to be registered withregister_taxonomy().
到 functions.php 文件中的 $args 數組,您可以使用 register_post_type()創建自定義帖子類型。
第三種解決方案
用這個:
add_action( 'init', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
register_taxonomy_for_object_type( 'post_tag', 'portfolio' );
};
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。