get_ancestors()

get_ancestors( int $object_id, string $object_type = ”,…

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

获取给定对象的祖先ID数组。
Get an array of ancestor IDs for a given object.

目录锚点:#参数#返回#源码#笔记


参数(Parameters)

参数 类型 必填 说明
$object_id (int) 可选 对象的ID。默认值0。
$object_type (string) 可选 我们要为其检索祖先的对象类型。接受post类型或分类名称。
$resource_type (string) 可选 资源$object的类型是。接受“post_type”或“taxonomy”。

返回(Return)

(int[])层次结构中从低到高的祖先id数组。


源码(Source)

/**
 * Get an array of ancestor IDs for a given object.
 *
 * @since 3.1.0
 * @since 4.1.0 Introduced the `$resource_type` argument.
 *
 * @param int    $object_id     Optional. The ID of the object. Default 0.
 * @param string $object_type   Optional. The type of object for which we'll be retrieving
 *                              ancestors. Accepts a post type or a taxonomy name. Default empty.
 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
 *                              or 'taxonomy'. Default empty.
 * @return array An array of ancestors from lowest to highest in the hierarchy.
 */
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);
	}

	/**
	 * Filter 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 );
}
更新版本 源码位置 使用 被使用
4.1.0 wp-includes/taxonomy.php:4501 4 7

笔记(Notes)

给定以下类别层次结构(带ID):
给定a页层次结构(带ID):

类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册