REST API 文档

💡 云策文档标注

概述

本文档介绍了 WordPress REST API 中 Nav_Menus 端点的数据结构与操作方法。核心内容包括菜单记录的字段定义、CRUD 操作的端点定义及参数说明,适用于开发者通过 API 管理导航菜单。

关键要点

  • 菜单记录包含 id、description、name、slug、meta、locations、auto_add 等字段,每个字段有 JSON 数据类型和上下文限制。
  • 支持通过 GET /wp/v2/menus 列出菜单,可配合 context、page、per_page、search 等参数进行过滤和分页。
  • 提供创建(POST /wp/v2/menus)、检索(GET /wp/v2/menus/<id>)、更新(POST /wp/v2/menus/<id>)和删除(DELETE /wp/v2/menus/<id>)菜单的端点,各有特定参数要求。
  • 操作涉及参数如 name(创建时必填)、force(删除时需设为 true),确保数据完整性和安全性。

代码示例

// 列出所有菜单的示例请求
$ curl https://example.com/wp-json/wp/v2/menus

// 删除特定菜单的示例请求
$ curl -X DELETE https://example.com/wp-json/wp/v2/menus/<id>

📄 原文内容

Schema

The schema defines all the fields that exist within a nav_menu record. Any response from these endpoints can be expected to contain the fields below unless the `_filter` query parameter is used or the schema field only appears in a specific context.

id Unique identifier for the term.

JSON data type: integer

Read only

Context: view, embed, edit

description HTML description of the term.

JSON data type: string

Context: view, edit

name HTML title for the term.

JSON data type: string

Context: view, embed, edit

slug An alphanumeric identifier for the term unique to its type.

JSON data type: string

Context: view, embed, edit

meta Meta fields.

JSON data type: object

Context: view, edit

locations The locations assigned to the menu.

JSON data type: array

Context: view, edit

auto_add Whether to automatically add top level pages to this menu.

JSON data type: boolean

Context: view, edit

List Nav_Menus

Query this endpoint to retrieve a collection of nav_menus. The response you receive can be controlled and filtered using the URL query parameters below.

Definition

GET /wp/v2/menus

Example Request

$ curl https://example.com/wp-json/wp/v2/menus

Arguments

context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit

page Current page of the collection.

Default: 1

per_page Maximum number of items to be returned in result set.

Default: 10

search Limit results to those matching a string.
exclude Ensure result set excludes specific IDs.
include Limit result set to specific IDs.
offset Offset the result set by a specific number of items.
order Order sort attribute ascending or descending.

Default: asc

One of: asc, desc

orderby Sort collection by term attribute.

Default: name

One of: id, include, name, slug, include_slugs, term_group, description, count

hide_empty Whether to hide terms not assigned to any posts.
post Limit result set to terms assigned to a specific post.
slug Limit result set to terms with one or more specific slugs.

Create a Nav_Menu

Arguments

<a href="#schema-description">description</a> HTML description of the term.
<a href="#schema-name">name</a> HTML title for the term.

Required: 1

<a href="#schema-slug">slug</a> An alphanumeric identifier for the term unique to its type.
<a href="#schema-meta">meta</a> Meta fields.
<a href="#schema-locations">locations</a> The locations assigned to the menu.
<a href="#schema-auto_add">auto_add</a> Whether to automatically add top level pages to this menu.

Definition

POST /wp/v2/menus

Retrieve a Nav_Menu

Definition & Example Request

GET /wp/v2/menus/<id>

Query this endpoint to retrieve a specific nav_menu record.

$ curl https://example.com/wp-json/wp/v2/menus/<id>

Arguments

id Unique identifier for the term.
context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit

Update a Nav_Menu

Arguments

<a href="#schema-id">id</a> Unique identifier for the term.
<a href="#schema-description">description</a> HTML description of the term.
<a href="#schema-name">name</a> HTML title for the term.
<a href="#schema-slug">slug</a> An alphanumeric identifier for the term unique to its type.
<a href="#schema-meta">meta</a> Meta fields.
<a href="#schema-locations">locations</a> The locations assigned to the menu.
<a href="#schema-auto_add">auto_add</a> Whether to automatically add top level pages to this menu.

Definition

POST /wp/v2/menus/<id>

Example Request

Delete a Nav_Menu

Arguments

id Unique identifier for the term.
force Required to be true, as terms do not support trashing.

Definition

DELETE /wp/v2/menus/<id>

Example Request

$ curl -X DELETE https://example.com/wp-json/wp/v2/menus/<id>