On this page
get_term_parents_list( int $term_id, string $taxonomy, string|array $args = array() ): string|WP_Error
Retrieves term parents with separator.
Parameters
$term_idint Required-
Term ID.
$taxonomystring Required-
Taxonomy name.
$argsstring|array Optional-
Array of optional arguments.
formatstringUse term names or slugs for display. Accepts'name'or'slug'.
Default'name'.separatorstringSeparator for between the terms. Default'/'.linkboolWhether to format as a link. Default true.inclusiveboolInclude the term to get the parents for. Default true.
Default:
array()
Return
string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
Source
File: wp-includes/category-template.php. View all references
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;
}
Related
Uses
| Uses | Description |
|---|---|
| wp_validate_boolean() wp-includes/functions.php | Filters/validates a variable as a boolean. |
| get_ancestors() wp-includes/taxonomy.php | Gets an array of ancestor IDs for a given object. |
| get_term_link() wp-includes/taxonomy.php | Generates a permalink for a taxonomy term archive. |
| esc_url() wp-includes/formatting.php | Checks and cleans a URL. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| get_term() wp-includes/taxonomy.php | Gets all term data from database by term ID. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| get_category_parents() wp-includes/category-template.php | Retrieves category parents with separator. |
Changelog
| Version | Description |
|---|---|
| 4.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_term_parents_list