最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

custom post types - CPT category hierarchy

programmeradmin1浏览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 );

?>

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 );

?>
Share Improve this question asked Apr 30, 2019 at 5:57 VuckoVucko 1631 gold badge1 silver badge10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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.

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

发布评论

评论列表(0)

  1. 暂无评论