我正在为Wordpress webservice使用WP REST API插件和Jetpack插件。所以,最初' Vehicle'是我的自定义帖子类型,它包括两种类型 分类学,即两轮车和四轮车。所以,目前我没有获得webservice结果中的分类法,即。 https://public-api.wordpress.com/rest/v1.1/sites/site_url/posts/?type=vehicle&status=publish&number=10&offset=0。 以下是我的代码。
/***************************************************
* Register a two-wheeler, four-wheeler taxonomy with Vehicle post type, with REST API support
***************************************************/
add_action( 'init', 'vehicle_group_taxonomy', 30 );
function vehicle_group_taxonomy() {
$labels = array(
'name' => _x( 'two_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'two_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search two_wheeler' ),
'all_items' => __( 'All two_wheeler' ),
'parent_item' => __( 'Parent two_wheeler' ),
'parent_item_colon' => __( 'Parent two_wheeler:' ),
'edit_item' => __( 'Edit two_wheeler' ),
'update_item' => __( 'Update two_wheeler' ),
'add_new_item' => __( 'Add New two_wheeler' ),
'new_item_name' => __( 'New two_wheeler Name' ),
'menu_name' => __( 'two_wheeler' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'two_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'two_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'two_wheeler', array( 'vehicle' ), $args );
$labels_speciality = array(
'name' => _x( 'four_wheeler', 'taxonomy general name' ),
'singular_name' => _x( 'four_wheeler', 'taxonomy singular name' ),
'search_items' => __( 'Search four_wheeler' ),
'all_items' => __( 'All four_wheeler' ),
'parent_item' => __( 'Parent four_wheeler' ),
'parent_item_colon' => __( 'Parent four_wheeler:' ),
'edit_item' => __( 'Edit four_wheeler' ),
'update_item' => __( 'Update four_wheeler' ),
'add_new_item' => __( 'Add New four_wheeler' ),
'new_item_name' => __( 'New four_wheeler Name' ),
'menu_name' => __( 'four_wheeler' ),
);
$args_speciality = array(
'hierarchical' => true,
'labels' => $labels_speciality,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'four_wheeler' ),
'show_in_rest' => true,
'rest_base' => 'four_wheeler',
'rest_controller_class' => 'WP_REST_Terms_Controller',
);
register_taxonomy( 'four_wheeler', array( 'vehicle' ), $args_speciality );
}
/********************************************************
* Add REST API support to an already registered post type.
*********************************************************/
add_action( 'init', 'my_custom_post_type_rest_support', 25 );
function my_custom_post_type_rest_support() {
global $wp_post_types;
//be sure to set this to the name of your post type!
$post_type_name = 'vehicle';
if( isset( $wp_post_types[ $post_type_name ] ) ) {
$wp_post_types[$post_type_name]->show_in_rest = true;
$wp_post_types[$post_type_name]->rest_base = $post_type_name;
$wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller';
}
}
/*******************************************************
* Add REST API support to an already registered taxonomy.
*********************************************************/
add_action( 'init', 'my_custom_taxonomy_rest_support', 25 );
function my_custom_taxonomy_rest_support() {
global $wp_taxonomies;
//be sure to set this to the name of your taxonomy!
$taxonomy_name = array('two_wheeler','four_wheeler');
if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) {
$wp_taxonomies[ $taxonomy_name ]->show_in_rest = true;
$wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name;
$wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller';
}
}