REST API 文档

💡 云策文档标注

概述

本文档详细介绍了 WordPress REST API 中 Template_Parts 端点的数据结构与操作。它定义了 template_part 记录的字段模式,并提供了创建、检索、更新和删除模板部分的端点定义、参数说明及示例请求。

关键要点

  • Schema 部分列出了 template_part 的所有字段,包括 id、slug、theme、type、content、title、description、status、wp_id、has_theme_file、author、modified 和 area,每个字段都标注了 JSON 数据类型、上下文和只读属性。
  • 支持通过 GET /wp/v2/template-parts 和 GET /wp/v2/template-parts/<id> 检索模板部分,参数如 context、wp_id、area 和 post_type 可用于筛选。
  • 创建模板部分使用 POST /wp/v2/template-parts,必需参数为 slug,可选参数包括 theme、type、content、title、description、status、author 和 area。
  • 更新模板部分使用 POST /wp/v2/template-parts/<id>,参数与创建类似,但 id 为必需。
  • 删除模板部分使用 DELETE /wp/v2/template-parts/<id>,参数包括 id 和可选的 force。

代码示例

// 检索所有模板部分
$ curl https://example.com/wp-json/wp/v2/template-parts

// 创建模板部分(示例请求)
POST /wp/v2/template-parts
参数:slug(必需),其他可选

// 删除模板部分
$ curl -X DELETE https://example.com/wp-json/wp/v2/template-parts/<id>

📄 原文内容

Schema

The schema defines all the fields that exist within a template_part 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 ID of template.

JSON data type: string

Read only

Context: embed, view, edit

slug Unique slug identifying the template.

JSON data type: string

Context: embed, view, edit

theme Theme identifier for the template.

JSON data type: string

Context: embed, view, edit

type Type of template.

JSON data type: string

Context: embed, view, edit

source Source of template

JSON data type: string

Read only

Context: embed, view, edit

origin Source of a customized template

JSON data type: string

Read only

Context: embed, view, edit

content Content of template.

JSON data type: object or string

Context: embed, view, edit

title Title of template.

JSON data type: object or string

Context: embed, view, edit

description Description of template.

JSON data type: string

Context: embed, view, edit

status Status of template.

JSON data type: string

Context: embed, view, edit

One of: publish, future, draft, pending, private

wp_id Post ID.

JSON data type: integer

Read only

Context: embed, view, edit

has_theme_file Theme file exists.

JSON data type: bool

Read only

Context: embed, view, edit

author The ID for the author of the template.

JSON data type: integer

Context: view, edit, embed

modified The date the template was last modified, in the site's timezone.

JSON data type: string,
Format: datetime (details)

Read only

Context: view, edit

area Where the template part is intended for use (header, footer, etc.)

JSON data type: string

Context: embed, view, edit

Retrieve a Template_Part

Definition & Example Request

GET /wp/v2/template-parts

Query this endpoint to retrieve a specific template_part record.

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

Arguments

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

Default: view

One of: view, embed, edit

wp_id Limit to the specified post id.
area Limit to the specified template part area.
post_type Post type to get the templates for.

Create a Template_Part

Arguments

<a href="#schema-slug">slug</a> Unique slug identifying the template.

Required: 1

<a href="#schema-theme">theme</a> Theme identifier for the template.
<a href="#schema-type">type</a> Type of template.
<a href="#schema-content">content</a> Content of template.
<a href="#schema-title">title</a> Title of template.
<a href="#schema-description">description</a> Description of template.
<a href="#schema-status">status</a> Status of template.

Default: publish

One of: publish, future, draft, pending, private

<a href="#schema-author">author</a> The ID for the author of the template.
<a href="#schema-area">area</a> Where the template part is intended for use (header, footer, etc.)

Definition

POST /wp/v2/template-parts

Retrieve a Template_Part

Definition & Example Request

GET /wp/v2/template-parts/<id>?)[/w%-]+)

Query this endpoint to retrieve a specific template_part record.

$ curl https://example.com/wp-json/wp/v2/template-parts/<id>?)[/w%-]+)

Arguments

id The id of a template
context Scope under which the request is made; determines fields present in response.

Default: view

One of: view, embed, edit

Update a Template_Part

Arguments

<a href="#schema-id">id</a> The id of a template
<a href="#schema-slug">slug</a> Unique slug identifying the template.
<a href="#schema-theme">theme</a> Theme identifier for the template.
<a href="#schema-type">type</a> Type of template.
<a href="#schema-content">content</a> Content of template.
<a href="#schema-title">title</a> Title of template.
<a href="#schema-description">description</a> Description of template.
<a href="#schema-status">status</a> Status of template.
One of: publish, future, draft, pending, private
<a href="#schema-author">author</a> The ID for the author of the template.
<a href="#schema-area">area</a> Where the template part is intended for use (header, footer, etc.)

Definition

POST /wp/v2/template-parts/<id>?)[/w%-]+)

Example Request

Delete a Template_Part

Arguments

id The id of a template
force Whether to bypass Trash and force deletion.

Definition

DELETE /wp/v2/template-parts/<id>?)[/w%-]+)

Example Request

$ curl -X DELETE https://example.com/wp-json/wp/v2/template-parts/<id>?)[/w%-]+)