目录
描述
Returns a list of the parents of a category, including the category, in hierarchical order.
用法
<?php get_category_parents( $id, $link, $separator, $nicename, $visited ); ?>
参数
$id
(integer) (必填) Category ID.
默认值: None
$link
(boolean) (可选) Whether to create a link to each category displayed.
默认值: false
$separator
(string) (可选) What to separate each category by.
默认值: '/'
$nicename
(boolean) (可选) Whether to use nice name for display.
默认值: false
$visited
(boolean) (可选) Already linked-to categories. This parameter is used internally by the function, which makes recursive calls to itself, to prevent duplicates in the returned list.
默认值: array()
返回值
(string|WP_Error)
The categories, separated by $separator. WP_Error on failure.
历史
- 添加于 版本: 1.2.0
源文件
get_category_parents() 函数的代码位于 wp-includes/category-template.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
/* ---------------------------------- * wordpress之魂 © http://wphun.com * ---------------------------------- */ /** * Retrieve category parents with separator. * * @since 1.2.0 * * @param int $id Category ID. * @param bool $link Optional, default is false. Whether to format with link. * @param string $separator Optional, default is '/'. How to separate categories. * @param bool $nicename Optional, default is false. Whether to use nice name for display. * @param array $visited Optional. Already linked to categories to prevent duplicates. * @return string|WP_Error A list of category parents on success, WP_Error on failure. */ function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) { $chain = ''; $parent = get_term( $id, 'category' ); if ( is_wp_error( $parent ) ) return $parent; if ( $nicename ) $name = $parent->slug; else $name = $parent->name; if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) { $visited[] = $parent->parent; $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited ); } if ( $link ) $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '">'.$name.'</a>' . $separator; else $chain .= $name.$separator; return $chain; } |
- 原文:http://codex.wordpress.org/Function_Reference/get_category_parents
- 翻译:黄聪@WordPress之魂