返回介绍

_get_term_children()

发布于 2017-09-11 13:25:02 字数 3859 浏览 981 评论 0 收藏 0

Alert: This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

_get_term_children( int $term_id,  array $terms,  string $taxonomy,  array $ancestors = array() )

Get the subset of $terms that are descendants of $term_id.


description

If $terms is an array of objects, then _get_term_children() returns an array of objects. If $terms is an array of IDs, then _get_term_children() returns an array of IDs.


参数

$term_id

(int) (Required) The ancestor term: all returned terms should be descendants of $term_id.

$terms

(array) (Required) The set of terms - either an array of term objects or term IDs - from which those that are descendants of $term_id will be chosen.

$taxonomy

(string) (Required) The taxonomy which determines the hierarchy of the terms.

$ancestors

(array) (Optional) Term ancestors that have already been identified. Passed by reference, to keep track of found terms when recursing the hierarchy. The array of located ancestors is used to prevent infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value.

Default value: array()


返回值

(array|WP_Error) The subset of $terms that are descendants of $term_id.


源代码

File: wp-includes/taxonomy.php

function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
	$empty_array = array();
	if ( empty($terms) )
		return $empty_array;

	$term_list = array();
	$has_children = _get_term_hierarchy($taxonomy);

	if  ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
		return $empty_array;

	// Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
	if ( empty( $ancestors ) ) {
		$ancestors[ $term_id ] = 1;
	}

	foreach ( (array) $terms as $term ) {
		$use_id = false;
		if ( !is_object($term) ) {
			$term = get_term($term, $taxonomy);
			if ( is_wp_error( $term ) )
				return $term;
			$use_id = true;
		}

		// Don't recurse if we've already identified the term as a child - this indicates a loop.
		if ( isset( $ancestors[ $term->term_id ] ) ) {
			continue;
		}

		if ( $term->parent == $term_id ) {
			if ( $use_id )
				$term_list[] = $term->term_id;
			else
				$term_list[] = $term;

			if ( !isset($has_children[$term->term_id]) )
				continue;

			$ancestors[ $term->term_id ] = 1;

			if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
				$term_list = array_merge($term_list, $children);
		}
	}

	return $term_list;
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/taxonomy.php: _get_term_children()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/load.php: is_wp_error()

Used By

  • wp-includes/class-wp-term-query.php: WP_Term_Query::get_terms()
  • wp-includes/taxonomy.php: _get_term_children()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文