操作指南
操作指南自定义标签

自定义标签

在 WordPress 中,我们可以为文章添加标签(即使用名为 "post_tag" 的分类法)。这已通过 PostTag 类型 映射到 GraphQL schema 中,并与 Post 条目关联。

同样,由任何主题或插件定义的自定义文章类型(例如 "product")也可以拥有与之关联的分类法标签(例如 "product-cat")。由于这些自定义文章类型未映射到 GraphQL schema,它们通过 GenericCustomPost 类型解析,其标签则解析为 GenericTag

我们使用 tagtags 字段来获取标签数据,通过字段参数 taxonomy 指定所引用的分类法。结果为联合类型 TagUnion,根据条目的分类法包含 PostTagGenericTag 的条目。

例如,以下 query 检索分类法为 "product-tag" 的标签:

query {
  tags(taxonomy: "product-tag") {
    __typename
 
    ...on Tag {
      count
      description
      id
      name
      slug
      url
    }
    
    ...on GenericTag {
      taxonomy   
      customPostCount
      customPosts {
        __typename
        ...on CustomPost {
          id
          title
        }
      }
    }
  }
}

允许访问未映射的标签分类法

可通过 GenericTag 类型访问的标签分类法,必须在插件设置页面中进行明确配置,具体说明请参阅指南 将自定义标签分类法添加到 schema