get_term_parents_list

函数
get_term_parents_list ( $term_id, $taxonomy, $args = array() )
参数
  • (int) $term_id Term ID.
    Required:
  • (string) $taxonomy Taxonomy name.
    Required:
  • (string|array) $args { Array of optional arguments. @type string $format Use term names or slugs for display. Accepts 'name' or 'slug'. Default 'name'. @type string $separator Separator for between the terms. Default '/'. @type bool $link Whether to format as a link. Default true. @type bool $inclusive Include the term to get the parents for. Default true. }
    Required:
    Default: array()
返回值
  • (string|WP_Error) A list of term parents on success, WP_Error or empty string on failure.
定义位置
相关方法
get_theme_feature_listget_category_parentsget_the_tag_listget_term_to_editget_the_term_list
引入
4.8.0
弃用
-

get_term_parents_list: 这个函数为一个给定的术语检索一个格式化的父术语(类别、标签等)的字符串。它需要三个参数:术语对象或ID,术语所属的分类法,以及父术语之间的分隔符。它以字符串形式返回格式化的父术语字符串。

检索带有分隔符的术语父母。

function get_term_parents_list( $term_id, $taxonomy, $args = array() ) {
	$list = '';
	$term = get_term( $term_id, $taxonomy );

	if ( is_wp_error( $term ) ) {
		return $term;
	}

	if ( ! $term ) {
		return $list;
	}

	$term_id = $term->term_id;

	$defaults = array(
		'format'    => 'name',
		'separator' => '/',
		'link'      => true,
		'inclusive' => true,
	);

	$args = wp_parse_args( $args, $defaults );

	foreach ( array( 'link', 'inclusive' ) as $bool ) {
		$args[ $bool ] = wp_validate_boolean( $args[ $bool ] );
	}

	$parents = get_ancestors( $term_id, $taxonomy, 'taxonomy' );

	if ( $args['inclusive'] ) {
		array_unshift( $parents, $term_id );
	}

	foreach ( array_reverse( $parents ) as $term_id ) {
		$parent = get_term( $term_id, $taxonomy );
		$name   = ( 'slug' === $args['format'] ) ? $parent->slug : $parent->name;

		if ( $args['link'] ) {
			$list .= '<a href="' . esc_url( get_term_link( $parent->term_id, $taxonomy ) ) . '">' . $name . '</a>' . $args['separator'];
		} else {
			$list .= $name . $args['separator'];
		}
	}

	return $list;
}

常见问题

FAQs
查看更多 >