获取给定对象的祖先ID数组。
原型
get_ancestors( int $object_id, string $object_type = '', string $resource_type = '' )
参数
$object_id
(int)
(Optional)
对象的ID。默认值为0。
$object_type
(string)
(Optional)
我们将检索祖先的对象类型。接受帖子类型或分类名称。
$resource_type
(string)
(Optional)
资源类型$ object_type是。接受’post_type’或’分类法’。
返回值
(array)
层次结构中从最低到最高的祖先数组。
源文件
路径:wp-includes/taxonomy.php
<?php
...
function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
$object_id = (int) $object_id;
$ancestors = array();
if ( empty( $object_id ) ) {
/** This filter is documented in wp-includes/taxonomy.php */
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
}
if ( ! $resource_type ) {
if ( is_taxonomy_hierarchical( $object_type ) ) {
$resource_type = 'taxonomy';
} elseif ( post_type_exists( $object_type ) ) {
$resource_type = 'post_type';
}
}
if ( 'taxonomy' === $resource_type ) {
$term = get_term($object_id, $object_type);
while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
$ancestors[] = (int) $term->parent;
$term = get_term($term->parent, $object_type);
}
} elseif ( 'post_type' === $resource_type ) {
$ancestors = get_post_ancestors($object_id);
}
/**
* Filters a given object's ancestors.
*
* @since 3.1.0
* @since 4.1.1 Introduced the `$resource_type` parameter.
*
* @param array $ancestors An array of object ancestors.
* @param int $object_id Object ID.
* @param string $object_type Type of object.
* @param string $resource_type Type of resource $object_type is.
*/
return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/get_ancestors/