Warning: Trying to access array offset on false in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 216
get_term_parents_list() - 检索父级项目以分隔符分隔 - WordPress函数 - WordPress动力
当前浏览:首页 / WordPress函数 / get_term_parents_list()

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

检索父级项目以分隔符分隔


Warning: Undefined array key -1 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 185

Warning: Undefined array key 4 in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/reference-cat.php on line 186

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "3eb3794598e48b6ab5a07d2289e3c38d" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "36030fe78ffd20bf6a789fd4064807d0" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

Warning: Undefined variable $searchand in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 154

Warning: Undefined array key "90276a70c1ce682ec49779101f60796b" in /www/wwwroot/www.wpdongli.com/wp-content/themes/WPdongli/functions/wp.php on line 223

listmore...

parent

term 分类项more...


参数

$term_id

(int) (必填) 分类法项目 ID(term ID)

$taxonomy

(string) (必填) 分类法名称(taxonomy name)

$args

(string|array) (可选) 可选参数数组

  • 'format'
    (string) 使用项目name还是slug进行显示,接受 'name' 或 'slug'. 默认 'name'.
  • 'separator'
    (string) 项目的分隔符,默认 '/'.
  • 'link'
    (bool) 是否为链接格式,默认 true.
  • 'inclusive'
    (bool) 返回的结果中是否包含子项目本身,默认 true.

默认值: array()


返回

(string|WP_Error) 成功返回项目的父项目列表,失败返回WP_Error或空字符串。



源码

查看源码 官方文档


更新日志

版本描述
4.8.0开始引入

使用示例

  • 示例1

    在类别存档页面中显示项目父级的示例。在主题类别存档模板中使用

    <?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' );
    }
    ?>
    

    这是一个在“子类别”项目页面上打印内容的示例。

    Parent Category/Child Category/Grandchild Category

  • 示例2

    分类页面的面包屑示例

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