0

I'm working on a _products catalog` WP theme. I have a issue where my CTP has multiple subcategories, like for example:

Vehicle -> Vehicle type -> Vehicle type -> Vehicle year -> Vehicle or

Car -> SUV -> VW -> 2018 -> T-Roc

How can I create that structure? For now my functions.php looks like:

<?php

function custom_post_type() {

    $labels = array(
        'name'                => _x( 'Cars', 'Post Type General Name', 'twentythirteen' ),
        'singular_name'       => _x( 'Car', 'Post Type Singular Name', 'twentythirteen' ),
        'menu_name'           => __( 'Cars', 'twentythirteen' ),
        'parent_item_colon'   => __( 'Parent Car', 'twentythirteen' ),
        'all_items'           => __( 'All Cars', 'twentythirteen' ),
        'view_item'           => __( 'View Car', 'twentythirteen' ),
        'add_new_item'        => __( 'Add New Car', 'twentythirteen' ),
        'add_new'             => __( 'Add New', 'twentythirteen' ),
        'edit_item'           => __( 'Edit Car', 'twentythirteen' ),
        'update_item'         => __( 'Update Car', 'twentythirteen' ),
        'search_items'        => __( 'Search Car', 'twentythirteen' ),
        'not_found'           => __( 'Not Found', 'twentythirteen' ),
        'not_found_in_trash'  => __( 'Not found in Trash', 'twentythirteen' ),
    );

    $args = array(
        'label'               => __( 'cars', 'twentythirteen' ),
        'description'         => __( 'Cars news and reviews', 'twentythirteen' ),
        'labels'              => $labels,
        'supports'            => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
        'taxonomies'          => array( 'category' ),
        'hierarchical'        => false,
        'public'              => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_nav_menus'   => true,
        'show_in_admin_bar'   => true,
        'menu_position'       => 5,
        'can_export'          => true,
        'has_archive'         => true,
        'exclude_from_search' => false,
        'publicly_queryable'  => true,
        'capability_type'     => 'page',
    );
    register_post_type( 'cars', $args );
}

add_action( 'init', 'custom_post_type', 0 );

?>
2

If you're trying to create a hierarchal structure with regards to custom post types you'll need to register a custom taxonomy on a more abstract version of your desired post type.

For example instead of naming your custom post type cars name it vehicle. You could then a register a hierarchical taxonomy with register_taxonomy(). In custom_post_type() you'd call register_taxonomy() as follows:

function custom_post_type() {
   // ... update labels to be more 'vehicle' friendly

   $args = [
     // ...
     'taxonomies' => [ 'vehicle_type' ] // name of the new taxonomy you'll be creating
     // ...
   ];

   register_post_type( 'vehicle', $args );

   $taxonomy_labels = [
     // ... entries here are very similar to labels for `register_post_type`
   ];

   $taxonomy_args = [
     'labels'       => $taxonomy_labels,
     'hierarchical' => TRUE // gives taxonomy a "category" like structure
   ];

   register_taxonomy( 'vehicle_type', $taxonomy_args );
}

add_action( 'init', 'custom_post_type', 0 );

NOTE: The name passed into register_taxonomy() MUST be the same as the entry in the taxonomies array in the args array for the registered post type() call for vehicle.

For more information on register_taxonomy see here.

Hope this helps.

2

WordPress doesn't support nested post types. See: custom taxonomy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.