get_term_children()获取一个分类的子分类

目录

描述

译文

将所有term子辈整合成一个单独数组。

该函数为递归函数,将$term的所有子辈整合到同一个数组中。仅对分层级的分类法有效。

若$taxonomy中没有$term则返回空数组。

原文

Merge all term children into a single array.

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

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

get_term_children() 描述

用法

<?php get_term_children$term$taxonomy ?>

get_term_children() 用法

参数

$term

(string) (必填) ID of Term to get children

默认值: None

$taxonomy

(string) (必填) Taxonomy Name

默认值: None

get_term_children() 参数

返回值

(array|WP_Error) 

Array of Term IDs. WP_Error returned if $taxonomy does not exist

get_term_children() 返回值

示例

A Basic Example

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

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?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.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<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_children() 示例

注意

  • 使用到: $wpdb
  • 使用到: _get_term_hierarchy()
  • 使用到: get_term_children Used to get the children of both $taxonomy and the parent $term

get_term_children() 注意

历史

添加于 版本: 2.3.0

get_term_children() 历史

源文件

get_term_children() 函数的代码位于 wp-includes/taxonomy.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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.
 *
 * @since 2.3.0
 *
 * @param string $term_id  ID of Term to get children.
 * @param string $taxonomy Taxonomy Name.
 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
 */
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;
}

get_term_children() 源文件

相关