我正在研究一个_products目录的WP主题。我遇到了一个问题,我的CTP有多个子类别,例如:
Vehicle -> Vehicle type -> Vehicle type -> Vehicle year -> Vehicle或
Car -> SUV -> VW -> 2018 -> T-Roc
我怎样才能创造出这样的结构?就目前而言,我的functions.php看起来是:
_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 );
?>发布于 2019-05-01 03:38:21
如果您试图创建有关自定义post类型的层次结构,则需要在所需post类型的更抽象版本上注册自定义分类法。
例如,不要命名自定义post类型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 );注意:传入register_taxonomy()的名称必须与args数组中registered post type()调用vehicle的条目相同。
有关register_taxonomy的更多信息,请参见这里。
希望这能有所帮助。
发布于 2019-04-30 11:54:55
WordPress不支持嵌套的post类型。请参阅:自定义分类法
https://wordpress.stackexchange.com/questions/336674
复制相似问题