返回介绍

get_term_parents_list()

发布于 2017-09-11 00:22:44 字数 4983 浏览 1490 评论 0 收藏 0

get_term_parents_list( int $term_id,  string $taxonomy,  string|array $args = array() )

Retrieve term parents with separator.


description


参数

$term_id

(int) (Required) Term ID.

$taxonomy

(string) (Required) Taxonomy name.

$args

(string|array) (Optional) Array of optional arguments.

  • 'format'
    (string) Use term names or slugs for display. Accepts 'name' or 'slug'. Default 'name'.
  • 'separator'
    (string) Separator for between the terms. Default '/'.
  • 'link'
    (bool) Whether to format as a link. Default true.
  • 'inclusive'
    (bool) Include the term to get the parents for. Default true.

Default value: array()


返回值

(string|WP_Error) A list of term parents on success, WP_Error or empty string on failure.


源代码

File: wp-includes/category-template.php

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

更新日志

Versiondescription
4.8.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: wp_validate_boolean()
  • wp-includes/formatting.php: esc_url()
  • wp-includes/functions.php: wp_parse_args()
  • wp-includes/taxonomy.php: get_ancestors()
  • wp-includes/taxonomy.php: get_term_link()
  • wp-includes/taxonomy.php: get_term()
  • wp-includes/load.php: is_wp_error()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/category-template.php: get_category_parents()

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 keesiemeijer

    Example to display term parents in category archive pages. Use this in your category archive theme templates.

    
    <?php
    if ( is_category() ) {
    	// Get the current category term id.
    	$query_obj = get_queried_object();
    	$term_id   = $query_obj->term_id;
    
    	echo get_term_parents_list( $term_id, 'category' );
    }
    ?>
    

    This is an example of what it would print on a “Grandchild Category” term page.

    Parent Category/Child Category/Grandchild Category

  2. Example of a breadcrumb trail for taxonomy pages. Use it in your theme’s taxonomy templates

    
    <?php
    if ( ( is_tax() || is_category() || is_tag() ) ) {
    	$trail     = '';
    	$home      = '/<a href="' . get_home_url() . '">Home</a>';
    	$query_obj = get_queried_object();
    	$term_id   = $query_obj->term_id;
    	$taxonomy  = get_taxonomy( $query_obj->taxonomy );
    
    	if ( $term_id && $taxonomy ) {
    		// Add taxonomy label name to the trail.
    		$trail .=  '/' . $taxonomy->labels->menu_name;
    		// Add term parents to the trail.
    		$trail .= '/' . get_term_parents_list( $term_id, $taxonomy->name, array( 'inclusive' => false ) );
    	}
    
    	// Print trail and add current term name at the end.
    	echo '<p class="breadcrumb-trail">' . $home . $trail . $query_obj->name . '</p>';
    }
    ?>
    

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

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

发布评论

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