🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
REST API可以使用与默认帖子类型或分类术语控制器相同的控制器为wp / v2命名空间中的自定义帖子类型和自定义分类法创建路线。 或者,您可以使用自己的控制器和命名空间。 本文将介绍如何使用自定义内容类型的API路由的默认控制器。 这是最简单的方法,并确保与第三方兼容的最高机会。 ##使用REST API支持注册自定义帖子类型 注册自定义帖子类型时,如果希望通过REST API可用,您应该在传递给register_post_type的参数中设置'show_in_rest'=> true。 将此参数设置为true将在wp / v2命名空间中添加路由。 ``` /** * Register a book post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type */ add_action( 'init', 'my_book_cpt' ); function my_book_cpt() { $args = array( 'public' => true, 'show_in_rest' => true, 'label' => 'Books' ); register_post_type( 'book', $args ); } ``` 您可以选择设置rest_base参数来更改基本URL,否则默认为该类型的名称。 在下面的示例中,“books”用作rest_base的值。 这将使路由的URL为wp-json / v2 / books,而不是wp-json / v2 / book /,这将是默认的。 另外,您可以传递rest_controller_class的参数。 这个类必须是WP_REST_Controller的子类。 默认情况下,WP_REST_Posts_Controller用作控制器。 如果您使用的是自定义控制器,则可能不在wp / v2命名空间内。 下面是一个注册一个post类型,一个完整的标签,对REST API的支持,一个定制的rest_base以及默认控制器的显式注册表的例子: ``` /** * Register a book post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type */ add_action( 'init', 'my_book_cpt' ); function my_book_cpt() { $labels = array( 'name' => _x( 'Books', 'post type general name', 'your-plugin-textdomain' ), 'singular_name' => _x( 'Book', 'post type singular name', 'your-plugin-textdomain' ), 'menu_name' => _x( 'Books', 'admin menu', 'your-plugin-textdomain' ), 'name_admin_bar' => _x( 'Book', 'add new on admin bar', 'your-plugin-textdomain' ), 'add_new' => _x( 'Add New', 'book', 'your-plugin-textdomain' ), 'add_new_item' => __( 'Add New Book', 'your-plugin-textdomain' ), 'new_item' => __( 'New Book', 'your-plugin-textdomain' ), 'edit_item' => __( 'Edit Book', 'your-plugin-textdomain' ), 'view_item' => __( 'View Book', 'your-plugin-textdomain' ), 'all_items' => __( 'All Books', 'your-plugin-textdomain' ), 'search_items' => __( 'Search Books', 'your-plugin-textdomain' ), 'parent_item_colon' => __( 'Parent Books:', 'your-plugin-textdomain' ), 'not_found' => __( 'No books found.', 'your-plugin-textdomain' ), 'not_found_in_trash' => __( 'No books found in Trash.', 'your-plugin-textdomain' ) ); $args = array( 'labels' => $labels, 'description' => __( 'Description.', 'your-plugin-textdomain' ), 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'book' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'show_in_rest' => true, 'rest_base' => 'books', 'rest_controller_class' => 'WP_REST_Posts_Controller', 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ) ); register_post_type( 'book', $args ); } ``` ## 使用REST API支持注册自定义分类法 使用REST API支持注册自定义分类法与注册自定义帖子类型非常相似:在传递给register_taxonomy的参数中传递'show_in_rest'=> true。 您可以选择通过rest_base更改分类法路由的基本URL。 分类法的默认控制器是WP_REST_Terms_Controller。 如果您选择使用自定义控制器,则可以使用rest_controller_class进行修改。 以下是使用REST API支持注册自定义分类法的示例: ``` /** * Register a genre post type, with REST API support * * Based on example at: https://codex.wordpress.org/Function_Reference/register_taxonomy */ add_action( 'init', 'my_book_taxonomy', 30 ); function my_book_taxonomy() { $labels = array( 'name' => _x( 'Genres', 'taxonomy general name' ), 'singular_name' => _x( 'Genre', 'taxonomy singular name' ), 'search_items' => __( 'Search Genres' ), 'all_items' => __( 'All Genres' ), 'parent_item' => __( 'Parent Genre' ), 'parent_item_colon' => __( 'Parent Genre:' ), 'edit_item' => __( 'Edit Genre' ), 'update_item' => __( 'Update Genre' ), 'add_new_item' => __( 'Add New Genre' ), 'new_item_name' => __( 'New Genre Name' ), 'menu_name' => __( 'Genre' ), ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'genre' ), 'show_in_rest' => true, 'rest_base' => 'genre', 'rest_controller_class' => 'WP_REST_Terms_Controller', ); register_taxonomy( 'genre', array( 'book' ), $args ); } ``` ## 向现有内容类型添加REST API支持 当您不能控制的代码(例如您正在使用的主题或插件)添加了自定义帖子类型或自定义分类法时,可能需要在已注册alredy之后添加REST API支持。 参数与前面的例子相同,但需要添加到全局$ wp_post_types和$ wp_taxonomies数组中。 以下是向现有自定义帖子类型添加REST API支持的示例: ``` /** * 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 = 'planet'; if( isset( $wp_post_types[ $post_type_name ] ) ) { $wp_post_types[$post_type_name]->show_in_rest = true; // Optionally customize the rest_base or controller class $wp_post_types[$post_type_name]->rest_base = $post_type_name; $wp_post_types[$post_type_name]->rest_controller_class = 'WP_REST_Posts_Controller'; } } ``` 以下是如何向已注册的自定义分类法添加REST API支持的示例。 ``` /** * 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 = 'planet_class'; if ( isset( $wp_taxonomies[ $taxonomy_name ] ) ) { $wp_taxonomies[ $taxonomy_name ]->show_in_rest = true; // Optionally customize the rest_base or controller class $wp_taxonomies[ $taxonomy_name ]->rest_base = $taxonomy_name; $wp_taxonomies[ $taxonomy_name ]->rest_controller_class = 'WP_REST_Terms_Controller'; } } ``` 如果您在执行这些示例中遇到问题,请确保您正在添加具有足够高优先级的这些钩子。 如果回调函数在发布类型或分类法被注册之前运行,则isset检查将阻止错误,但不会添加支持。 ##自定义链接关系 分类和自定义帖子类型在WordPress内部有一个内置关联,但是如果要在两个自定义帖子类型之间建立链接呢? 这在WordPress本身不正式支持,但是我们可以使用_link关系在任意内容类型之间创建我们自己的连接