给定一个术语ID,如何使用getEntityRecords通过其ID获取术语对象?
我有一个自定义的分类与鼻涕虫genre。
getEntityRecords( 'taxonomy', 'genre' );目前,这检索了类型下的所有术语,但我想检索与ID匹配的术语。我能在上面函数的某个地方传递分类法ID吗?
发布于 2021-03-03 16:02:52
因此,getEntityRecords( 'taxonomy', 'genre' )将向REST中的/wp/v2/genre (如果您的分类法使用自定义rest_base)的"list terms“端点发出请求(如果您的分类法使用自定义C3),并且由于自定义分类法的”列表术语“端点默认使用/wp/v2/categories使用的相同参数(内置category分类法的”列表术语“端点),如果您希望将结果集限制为特定的术语in,那么可以使用include参数:
const termId = 123;
// The optional third parameter is an object which contains arguments for the
// specific REST API endpoint. On successful requests, this will be an array of
// term objects.
const terms = getEntityRecords( 'taxonomy', 'genre', { include: [ termId ] } );
console.log( terms && terms[0] ? terms[0].name : terms );但是,与使用getEntityRecords()不同,您可能只想使用getEntityRecord()来获取单个术语的对象/数据:
const termId = 123;
// The third parameter is mandatory and it is the term ID.
const term = getEntityRecord( 'taxonomy', 'genre', termId );
console.log( term?.name );如果您还不知道,可以向/wp/v2 (例如https://example.com/wp-json/wp/v2)请求查看所有已注册的路由和端点,以及每个端点的参数。
https://wordpress.stackexchange.com/questions/384394
复制相似问题