admin管理员组文章数量:1125317
I am trying to add a Custom Taxonomy to Woocommerce by targeting the product post type in Woocommerce. I used following code and added it into functions.php
I am not getting any error message but the taxonomy is not showing in the Woocommerce as well. Can you please let me know how I can do this or what I am doing wrong here?
<?php
// Register Custom Taxonomy
function custom_taxonomy_Item() {
$labels = array(
'name' => 'Items',
'singular_name' => 'Item',
'menu_name' => 'Item',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Item with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove Items',
'choose_from_most_used' => 'Choose from the most used Items',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'item', 'product', $args );
}
?>
Update
<?php
add_action( 'init', 'custom_taxonomy_Item' );
// Register Custom Taxonomy
function custom_taxonomy_Item() {
$labels = array(
'name' => 'Items',
'singular_name' => 'Item',
'menu_name' => 'Item',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Item with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove Items',
'choose_from_most_used' => 'Choose from the most used Items',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy_for_object_type( 'item', 'product', $args );
}
?>
I am trying to add a Custom Taxonomy to Woocommerce by targeting the product post type in Woocommerce. I used following code and added it into functions.php
I am not getting any error message but the taxonomy is not showing in the Woocommerce as well. Can you please let me know how I can do this or what I am doing wrong here?
<?php
// Register Custom Taxonomy
function custom_taxonomy_Item() {
$labels = array(
'name' => 'Items',
'singular_name' => 'Item',
'menu_name' => 'Item',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Item with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove Items',
'choose_from_most_used' => 'Choose from the most used Items',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'item', 'product', $args );
}
?>
Update
<?php
add_action( 'init', 'custom_taxonomy_Item' );
// Register Custom Taxonomy
function custom_taxonomy_Item() {
$labels = array(
'name' => 'Items',
'singular_name' => 'Item',
'menu_name' => 'Item',
'all_items' => 'All Items',
'parent_item' => 'Parent Item',
'parent_item_colon' => 'Parent Item:',
'new_item_name' => 'New Item Name',
'add_new_item' => 'Add New Item',
'edit_item' => 'Edit Item',
'update_item' => 'Update Item',
'separate_items_with_commas' => 'Separate Item with commas',
'search_items' => 'Search Items',
'add_or_remove_items' => 'Add or remove Items',
'choose_from_most_used' => 'Choose from the most used Items',
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy_for_object_type( 'item', 'product', $args );
}
?>
Share
Improve this question
edited Dec 1, 2013 at 7:16
Maruti Mohanty
2,4041 gold badge19 silver badges21 bronze badges
asked Nov 25, 2013 at 20:34
user1760110user1760110
2171 gold badge2 silver badges9 bronze badges
2
|
1 Answer
Reset to default 19You have to do:
add_action( 'init', 'custom_taxonomy_Item' );
Because:
Use the init action to call this function. Calling it outside of an action can lead to troubles.
see codex page register_taxonomy. Besides that :
Better be safe than sorry when registering custom taxonomies for custom post types. Use register_taxonomy_for_object_type() right after the function to interconnect them. Else you could run into minetraps where the post type isn't attached inside filter callback that run during
parse_request
orpre_get_posts
.
So better add:
register_taxonomy_for_object_type( 'item', 'product' );
Additionally to reading the linked codex pages you could take a look at:
- Use standard WordPress categories with a CPT
All this together should get you started.
**Edit:**
Like I said in the comment, it's working for me, this is the Code:
add_action( 'init', 'custom_taxonomy_Item' );
function custom_taxonomy_Item() {
$labels = array(
'name' => __('Items'),
'singular_name' => __('Item'),
'menu_name' => __('Items'),
'all_items' => __('All Items'),
'parent_item' => __('Parent Item'),
'parent_item_colon' => __('Parent Item:'),
'new_item_name' => __('New Item Name'),
'add_new_item' => __('Add New Item'),
'edit_item' => __('Edit Item'),
'update_item' => __('Update Item'),
'separate_items_with_commas' => __('Separate Item with commas'),
'search_items' => __('Search Items'),
'add_or_remove_items' => __('Add or remove Items'),
'choose_from_most_used' => __('Choose from the most used Items'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'item', 'product', $args );
register_taxonomy_for_object_type( 'item', 'product' );
}
本文标签: How to Add Custom Taxonomy To Woocommerce Plugin
版权声明:本文标题:How to Add Custom Taxonomy To Woocommerce Plugin 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736659113a1946330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
custom_taxonomy_Item
function to? – Milo Commented Nov 25, 2013 at 20:39