WordPress开发者和主题制作者的搜索引擎



get_term_parents_list ›

Since4.8.0
已弃用n/a
get_term_parents_list ( $term_id, $taxonomy, $args = array() )
参数: (3)
  • (int) $term_id Term ID.
    Required: Yes
  • (string) $taxonomy Taxonomy name.
    Required: Yes
  • (string|array) $args { Array of optional arguments.
    Required: No
    默认: array()
返回:
  • (string|WP_Error) A list of term parents on success, WP_Error or empty string on failure.
定义在:
文档:

Retrieve term parents with separator.



源码

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