返回介绍

get_term_children()

发布于 2017-09-11 00:21:35 字数 3456 浏览 1584 评论 0 收藏 0

get_term_children( string $term_id,  string $taxonomy )

Merge all term children into a single array of their IDs.


description

This recursive function will merge all of the children of $term into the same array of term IDs. Only useful for taxonomies which are hierarchical.

Will return an empty array if $term does not exist in $taxonomy.


参数

$term_id

(string) (Required) ID of Term to get children.

$taxonomy

(string) (Required) Taxonomy Name.


返回值

(array|WP_Error) List of Term IDs. WP_Error returned if $taxonomy does not exist.


源代码

File: wp-includes/taxonomy.php

function get_term_children( $term_id, $taxonomy ) {
	if ( ! taxonomy_exists( $taxonomy ) ) {
		return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
	}

	$term_id = intval( $term_id );

	$terms = _get_term_hierarchy($taxonomy);

	if ( ! isset($terms[$term_id]) )
		return array();

	$children = $terms[$term_id];

	foreach ( (array) $terms[$term_id] as $child ) {
		if ( $term_id == $child ) {
			continue;
		}

		if ( isset($terms[$child]) )
			$children = array_merge($children, get_term_children($child, $taxonomy));
	}

	return $children;
}

更新日志

Versiondescription
2.3.0Introduced.

相关函数

Uses

  • wp-includes/l10n.php: __()
  • wp-includes/taxonomy.php: get_term_children()
  • wp-includes/taxonomy.php: taxonomy_exists()
  • wp-includes/class-wp-error.php: WP_Error::__construct()

Used By

  • wp-includes/class-wp-term-query.php: WP_Term_Query::get_terms()
  • wp-includes/class-wp-tax-query.php: WP_Tax_Query::clean_query()
  • wp-includes/taxonomy.php: get_term_children()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by Codex

    A Basic Example
    Used to get an array of children taxonomies and write them out with links in an unordered list.

    
    <?php
    $term_id = 10;
    $taxonomy_name = 'products';
    $termchildren = get_term_children( $term_id, $taxonomy_name );
    
    echo '<ul>';
    foreach ( $termchildren as $child ) {
    	$term = get_term_by( 'id', $child, $taxonomy_name );
    	echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
    }
    echo '</ul>';
    ?> 
    

    This would return something like.

    
    <ul> 
    <li><a href="link_to_term_page">Term 1</a></li>
    <li><a href="link_to_term_page">Term 2</a></li>
    </ul>
    
    

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

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

发布评论

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