get_term_children() - WordPress 函数参考中文文档

返回介绍

get_term_children()

发布于 2017-09-11 字数3458 浏览 1285 评论 0

get_term_children( string $term_id,  string $taxonomy )

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


描述

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;
}

更新日志

Version 描述
2.3.0 Introduced.

相关函数

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>
    
    

上一篇:get_term_by()

下一篇:get_term_feed_link()

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

目前还没有任何评论,快来抢沙发吧!