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
  • what action have you hooked your custom_taxonomy_Item function to? – Milo Commented Nov 25, 2013 at 20:39
  • Honestly, I didn't know what action to hook? I couldnt find which part of plugin in handling this? – user1760110 Commented Nov 25, 2013 at 20:46
Add a comment  | 

1 Answer 1

Reset to default 19

You 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 or pre_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