首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CPT范畴层次结构

CPT范畴层次结构
EN

WordPress Development用户
提问于 2019-04-30 05:57:56
回答 2查看 151关注 0票数 0

我正在研究一个_products目录的WP主题。我遇到了一个问题,我的CTP有多个子类别,例如:

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

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

我怎样才能创造出这样的结构?就目前而言,我的functions.php看起来是:

代码语言:javascript
复制
 _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 );

?>
EN

回答 2

WordPress Development用户

回答已采纳

发布于 2019-05-01 03:38:21

如果您试图创建有关自定义post类型的层次结构,则需要在所需post类型的更抽象版本上注册自定义分类法。

例如,不要命名自定义post类型cars,而是将其命名为vehicle。然后,您可以使用register_taxonomy()注册一个分层分类法。在custom_post_type()中,您可以按以下方式调用register_taxonomy()

代码语言:javascript
复制
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 );

注意:传入register_taxonomy()的名称必须与args数组中registered post type()调用vehicle的条目相同。

有关register_taxonomy的更多信息,请参见这里

希望这能有所帮助。

票数 2
EN

WordPress Development用户

发布于 2019-04-30 11:54:55

WordPress不支持嵌套的post类型。请参阅:自定义分类法

票数 2
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/336674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档