CPT category hierarchy

时间:2019-04-30 作者:Vucko

我正在研究产品目录的WP主题。我的CTP有多个子类别,例如:

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

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

如何创建该结构?现在我的functions.php 看起来像:

<?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 个回复
最合适的回答,由SO网友:ces_1094 整理而成

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

例如,不命名自定义帖子类型cars 命名它vehicle. 然后,您可以使用register_taxonomy(). 在里面custom_post_type() 你会打电话的register_taxonomy() 具体如下:

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: 传入的名称register_taxonomy() 必须与中的条目相同taxonomies 中的数组args 的数组registered post type() 需要vehicle.

有关register_taxonomy 看见here.

希望这有帮助。

SO网友:Roy van Wensen

WordPress不支持嵌套的帖子类型。请参见:custom taxonomy

相关推荐

Fixing external image urls

我的图片目前已外部链接到我的博客帐户。通过PowerPress导入所有内容后,它导入了我的所有图像,如下所示:<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"https://1.bp.blogspot.com/-YrjQLYBicjE/Wl19tTXO1mI/AAAAAAAAneY/ulQf9voxKGo0CTZFb4Xi96Lk0mFS2purwCLcBGAs/s1600/95D