WordPress获取祖先ID函数:get_ancestors

WordPress函数get_ancestors可以用于获取分类的祖先ID,或页面的祖先ID,包括所有的上级。

get_ancestors( int $object_id, string $object_type = '', string $resource_type = '' )

函数参数

$object_id

整数

对象ID

$object_type

字符串

对象类型,可用值:page、post、category或其他分类法名称

$resource_type

字符串

资源的类型,可用值:post_type、taxonomy

函数使用示例

获取ID为7的分类的所有祖先分类:

$ids = get_ancestors(7, 'category');

获取ID为15的所有祖先页面:

$ids = get_ancestors(15, 'page');

扩展阅读

get_ancestors()函数位于:wp-includes/taxonomy.php

相关函数:

阿里云