內容簡介
截至版本 1.1.1,JSON REST API(WP API) 不允許用戶指定標籤或類別,在創建或編輯文章時(似乎後續版本會加入此功能)。此外掛解決了此問題,允許在創建/編輯文章時在 JSON 資料中傳遞類別和標籤。
可使用JSON REST API(WP API)製作的一個範例 JSON 文章(詳細說明此處),創建 WordPress 文章的內容如下:
{"title":"Hello World!","content_raw":"Content","excerpt_raw":"Excerpt"}
要添加類別或標籤,您需要在 JSON 資料中添加一個名為“x-categories”的陣列或一個名為“x-tags”的陣列,例如:
{"title":"Hello World!","content_raw":"Content","excerpt_raw":"Excerpt","x-tags":["tag1","tag2"],"x-categories":["General","15"]}
此範例創建了一篇文章,並分配了標籤“tag1”和“tag2”,將文章置於名為“General”的類別和類別ID 15中。標籤不必在使用前存在。類別必須在使用前存在,並且可以表達為類別名稱或類別 ID。在這個外掛中使用“x-”前綴用於避免與JSON REST API(WP API)外掛功能之間的任何衝突。
因為外掛依賴JSON REST API(WP API)提供的 JSON 解析功能,因此使用多部分表單主體的替代語法也應該有效。
默認情況下,編輯文章時指定的類別或標籤將取代已分配給文章的任何類別或標籤。您可以更改此行為,使類別或標籤附加而不是替換,只需打開 WordPress 插件文件夾中的“json-rest-api-wp-api-categories-and-tags”文件夾,編輯“json-rest-api-patch.php”即可。在第12行上可以找到一個變數,應將其設置為true,才能附加而不是替換標籤或類別。
外掛標籤
開發者團隊
② 後台搜尋「JSON REST API (WP API) Categories and Tags」→ 直接安裝(推薦)
原文外掛簡介
The JSON REST API (WP API) as of version 1.1.1 does not allow you to specify tags or categories
when createing or editing posts (it appears this will be added in a later release). This plugin addresses that problem by allowing categories and tags to be
passed in the JSON data when creating/editing posts.
An example JSON post that can be made using the JSON REST API (WP API) (described in detail here) to create a post in WordPress would be:
{"title":"Hello World!","content_raw":"Content","excerpt_raw":"Excerpt"}
In order to add tags/categories you would add an array called “x-categories” and/or and array called “x-tags” to the JSON data, for example:
{"title":"Hello World!","content_raw":"Content","excerpt_raw":"Excerpt","x-tags":["tag1","tag2"],"x-categories":["General","15"]}
The example creates a post and assignes the tags “tag1” and “tag2” as well as placing the post in the category named “General” and category ID 15. The tags do not have to exist prior to use. Categories must exist prior to use and may be expressed and either category names or category IDs. The “x-” prefixes were used to avoid any conflicts in functionality with the JSON REST API (WP API) plugin.
Because this plugin relies on the JSON parsing functionallity provided by the JSON REST API (WP API) plugin the alternate syntax using a multi-part-form body should also work.
By default the tags/categories specified when editing a post will replace any tags/categories already assigned to the post. This behavior can be changed so that tags/categories are appened instead, simply open the json-rest-api-wp-api-categories-and-tags folder in the WordPress plugins folder and edit json-rest-api-patch.php. On
line 12 of the file is a variable that should be set to true in order to have tags/categories appended instead of replaced.
