操作指南自定义分类
自定义分类
在 WordPress 中,我们可以为文章添加分类(即使用名为 "category" 的分类法)。这已通过 PostCategory 类型 映射到 GraphQL schema 中,并与 Post 条目相关联。
同样,由主题或插件定义的自定义文章类型(例如 "product")也可以关联其自身的分类法(例如 "product-cat")。由于这些自定义文章类型未映射到 GraphQL schema,它们通过 GenericCustomPost 类型进行解析,其分类则解析为 GenericCategory。
我们使用 category 和 categories 字段来获取分类数据,并通过字段参数 taxonomy 指定所引用的分类法。结果为联合类型 CategoryUnion,包含来自 PostCategory 或 GenericCategory 的条目(取决于条目的分类法)。
例如,以下 query 检索分类法为 "product-category" 的分类:
query {
categories(taxonomy: "product-category") {
__typename
...on Category {
count
description
id
name
slug
url
}
...on GenericCategory {
taxonomy
customPostCount
customPosts {
__typename
...on CustomPost {
id
title
}
}
}
}
}允许访问未映射的分类法
可通过 GenericCategory 类型访问的分类法必须在插件设置页面中进行明确配置,详情请参阅指南 将自定义分类法添加到 schema。