Custom Taxonomy inside another Custom Taxonomy in Wordpress

Asked

Viewed 97 times

1

I need to create the following hierarchy within the Wordpress admin:

Category -> Product Line -> Product

A product belongs to a line which in turn belongs to a category.

I thought I’d create a Custom Taxonomy for Categories, one for Lines. Populate them with the necessary data.

What I need to know if it is possible, is when creating a product (post), if there is any way to pull inside a select the categories created and the lines created to insert into the product.

Is there such a possibility?

Thank you.

1 answer

0

You can make the products as custom posts and create the taxomonias and terms for it.

EX:

Category -> subcategory -> post

Category -> product line -> product

Both running in parallel and no need to modify anything when registering the product, since it will respond to their own categories and terms.

To create the custom of what you need to put this in functions.php:

add_action('init', 'type_post_produto');
function type_post_produto() { 
            $labels = array(
                'name' => _x('Produto', 'post type general name'),
                'singular_name' => _x('Produto', 'post type singular name'),
                'add_new' => _x('Adicionar Novo Produto', 'Novo Produto'),
                'add_new_item' => __('Novo Produto'),
                'edit_item' => __('Editar Produto'),
                'new_item' => __('Novo Produto'),
                'view_item' => __('Ver Produto'),
                'search_items' => __('Procurar Produto'),
                'not_found' =>  __('Nenhum registro encontrado'),
                'not_found_in_trash' => __('Nenhum registro encontrado na lixeira'),
                'parent_item_colon' => '',
                'menu_name' => 'Produtos'
            );

            $args = array(
                'labels' => $labels,
                'public' => true,
                'public_queryable' => true,
                'show_ui' => true,      
                'query_var' => true,
                'taxonomies' => array( 'Categoria' ),
                'rewrite' => array('slug' => 'produto'),
                'capability_type' => 'post',
                'has_archive' => true,
                'hierarchical' => false,
                'menu_position' => null, 
                'supports' => array('title', 'editor', 'thumbnail', 'revisions'),
            );

        register_post_type( 'produtos' , $args );
        flush_rewrite_rules();
        }

With this you will only need to create product category hierarchies and create custom loopings to retrieve the "products".

Browser other questions tagged

You are not signed in. Login or sign up in order to post.