REST API 可以在命名空间内为自定义帖子类型和自定义分类法创建路由wp/v2
,使用与默认帖子类型或分类术语控制器相同的控制器。或者,您可以使用自己的控制器和命名空间。本文档将介绍如何为自定义内容类型的 API 路由使用默认控制器。这是最简单的方法,可确保最大程度地与第三方兼容。
使用 REST API 支持注册自定义帖子类型
注册自定义帖子类型时,如果您希望它通过 REST API 可用,您应该'show_in_rest' => true
在传递给register_post_type
. 将此参数设置为 true 将在wp/v2
命名空间中添加一个路由。
<span class="token php language-php"><span class="token comment">/**
* Register a book post type, with REST API support
*
* Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type
*/</span>
<span class="token function">add_action</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'init'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_book_cpt'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">my_book_cpt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$args</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_in_rest'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'label'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Books'</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">register_post_type</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'book'</span><span class="token punctuation">,</span> <span class="token variable">$args</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span>
您可以选择设置rest_base
参数以更改基本 url,否则默认为帖子类型的名称。在下面的示例中,“books”用作 的值rest_base
。这将使路由的 URLwp-json/wp/v2/books
而不是wp-json/wp/v2/book/
默认的 URL。
此外,您可以为 传递参数rest_controller_class
。此类必须是 的子类WP_REST_Controller
。默认情况下,WP_REST_Posts_Controller
用作控制器。如果您使用的是自定义控制器,那么您很可能不在wp/v2
命名空间内。
这是一个注册帖子类型的示例,具有完整标签、对 REST API 的支持、自定义的 rest_base 和默认控制器的显式注册:
<span class="token php language-php"><span class="token comment">/**
* Register a book post type, with REST API support
*
* Based on example at: https://codex.wordpress.org/Function_Reference/register_post_type
*/</span>
<span class="token function">add_action</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'init'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_book_cpt'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">my_book_cpt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$labels</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Books'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'post type general name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'singular_name'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'post type singular name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'menu_name'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Books'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'admin menu'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'name_admin_bar'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'add new on admin bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'add_new'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Add New'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'add_new_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Add New Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'new_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'New Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'edit_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Edit Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'view_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'View Book'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'all_items'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'All Books'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'search_items'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Search Books'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'parent_item_colon'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Parent Books:'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'not_found'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'No books found.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'not_found_in_trash'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'No books found in Trash.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$args</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'labels'</span> <span class="token operator">=></span> <span class="token variable">$labels</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'description'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Description.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'your-plugin-textdomain'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'public'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'publicly_queryable'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_ui'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_in_menu'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'query_var'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rewrite'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'slug'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'book'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'capability_type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'has_archive'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'hierarchical'</span> <span class="token operator">=></span> <span class="token constant boolean">false</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'menu_position'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_in_rest'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rest_base'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'books'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rest_controller_class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'WP_REST_Posts_Controller'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'supports'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'title'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'editor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'author'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'thumbnail'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'excerpt'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'comments'</span> <span class="token punctuation">)</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">register_post_type</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'book'</span><span class="token punctuation">,</span> <span class="token variable">$args</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span>
如果您使用的是自定义rest_controller_class
,则 REST API 无法自动确定给定帖子的路由。在这种情况下,您可以使用rest_route_for_post
过滤器来提供此信息。这允许您的自定义帖子类型在搜索端点中正确格式化,并启用自动发现链接。
<span class="token php language-php"><span class="token keyword">function</span> <span class="token function-definition function">my_plugin_rest_route_for_post</span><span class="token punctuation">(</span> <span class="token variable">$route</span><span class="token punctuation">,</span> <span class="token variable">$post</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token variable">$post</span><span class="token operator">-></span><span class="token property">post_type</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'book'</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$route</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/wp/v2/books/'</span> <span class="token operator">.</span> <span class="token variable">$post</span><span class="token operator">-></span><span class="token constant">ID</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token variable">$route</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'rest_route_for_post'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_plugin_rest_route_for_post'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>
使用 REST API 支持注册自定义分类法
注册具有 REST API 支持的自定义分类法与注册自定义帖子类型非常相似:将传递'show_in_rest' => true
给register_taxonomy
. 您可以选择传递rest_base
以更改分类路由的基本 url。
分类法的默认控制器是WP_REST_Terms_Controller
. rest_controller_class
如果您选择使用自定义控制器,您可以使用 修改它。
以下示例说明如何使用 REST API 支持注册自定义分类法:
<span class="token php language-php"><span class="token comment">/**
* Register a genre post type, with REST API support
*
* Based on example at: https://codex.wordpress.org/Function_Reference/register_taxonomy
*/</span>
<span class="token function">add_action</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'init'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_book_taxonomy'</span><span class="token punctuation">,</span> <span class="token number">30</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">my_book_taxonomy</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$labels</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Genres'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taxonomy general name'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'singular_name'</span> <span class="token operator">=></span> <span class="token function">_x</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Genre'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'taxonomy singular name'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'search_items'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Search Genres'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'all_items'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'All Genres'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'parent_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Parent Genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'parent_item_colon'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Parent Genre:'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'edit_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Edit Genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'update_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Update Genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'add_new_item'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Add New Genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'new_item_name'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'New Genre Name'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'menu_name'</span> <span class="token operator">=></span> <span class="token function">__</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'Genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$args</span> <span class="token operator">=</span> <span class="token keyword">array</span><span class="token punctuation">(</span>
<span class="token string single-quoted-string">'hierarchical'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'labels'</span> <span class="token operator">=></span> <span class="token variable">$labels</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_ui'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_admin_column'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'query_var'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rewrite'</span> <span class="token operator">=></span> <span class="token keyword">array</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'slug'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'genre'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'show_in_rest'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rest_base'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'genre'</span><span class="token punctuation">,</span>
<span class="token string single-quoted-string">'rest_controller_class'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'WP_REST_Terms_Controller'</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">register_taxonomy</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'genre'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'book'</span> <span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token variable">$args</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span>
如果您使用的是自定义rest_controller_class
,则 REST API 无法自动确定给定术语的路由。在这种情况下,您可以使用rest_route_for_term
过滤器来提供此信息。这允许您的自定义分类法在搜索端点中正确格式化并启用自动发现链接。
<span class="token php language-php"><span class="token keyword">function</span> <span class="token function-definition function">my_plugin_rest_route_for_term</span><span class="token punctuation">(</span> <span class="token variable">$route</span><span class="token punctuation">,</span> <span class="token variable">$term</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token variable">$term</span><span class="token operator">-></span><span class="token property">taxonomy</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'genre'</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$route</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/wp/v2/genre/'</span> <span class="token operator">.</span> <span class="token variable">$term</span><span class="token operator">-></span><span class="token property">term_id</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token variable">$route</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'rest_route_for_term'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_plugin_rest_route_for_term'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</span>
向现有内容类型添加 REST API 支持
如果您需要为您无法控制的自定义帖子类型或自定义分类添加 REST API 支持,例如您正在使用的主题或插件,您可以使用自register_post_type_args
WordPress 4.6.0 版本以来存在的过滤器挂钩。
<span class="token php language-php"><span class="token comment">/**
* Add REST API support to an already registered post type.
*/</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'register_post_type_args'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_post_type_args'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">my_post_type_args</span><span class="token punctuation">(</span> <span class="token variable">$args</span><span class="token punctuation">,</span> <span class="token variable">$post_type</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token string single-quoted-string">'book'</span> <span class="token operator">===</span> <span class="token variable">$post_type</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'show_in_rest'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span>
<span class="token comment">// Optionally customize the rest_base or rest_controller_class</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'rest_base'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'books'</span><span class="token punctuation">;</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'rest_controller_class'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'WP_REST_Posts_Controller'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token variable">$args</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span>
对于自定义分类法,它几乎是一样的。您可以使用register_taxonomy_args
自 WordPress 4.4.0 版本以来存在的过滤器。
<span class="token php language-php"><span class="token comment">/**
* Add REST API support to an already registered taxonomy.
*/</span>
<span class="token function">add_filter</span><span class="token punctuation">(</span> <span class="token string single-quoted-string">'register_taxonomy_args'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my_taxonomy_args'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">2</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">function</span> <span class="token function-definition function">my_taxonomy_args</span><span class="token punctuation">(</span> <span class="token variable">$args</span><span class="token punctuation">,</span> <span class="token variable">$taxonomy_name</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span> <span class="token string single-quoted-string">'genre'</span> <span class="token operator">===</span> <span class="token variable">$taxonomy_name</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'show_in_rest'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span>
<span class="token comment">// Optionally customize the rest_base or rest_controller_class</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'rest_base'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'genres'</span><span class="token punctuation">;</span>
<span class="token variable">$args</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'rest_controller_class'</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'WP_REST_Terms_Controller'</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">return</span> <span class="token variable">$args</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</span>
自定义链接关系
分类法和自定义帖子类型在 WordPress 中具有内置关联,但是如果您想在两个自定义帖子类型之间建立链接怎么办?这在 WordPress 本身中不受正式支持,但我们可以使用关系在任意内容类型之间创建我们自己的连接_link
。