The get_term_parents_list() WordPress PHP function retrieves a list of term parents separated by a specified separator.
Usage
get_term_parents_list($term_id, $taxonomy, $args)
Parameters
$term_id (int)– Required. The ID of the term.$taxonomy (string)– Required. The 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.
More information
See WordPress Developer Resources: get_term_parents_list()
Examples
Display Term Parents in Category Archive Pages
This example displays term parents in category archive pages. Use this in your category archive theme templates.
if (is_category()) {
// Get the current category term id
$query_obj = get_queried_object();
$term_id = $query_obj->term_id;
// Echo term parents list
echo get_term_parents_list($term_id, 'category');
}
Breadcrumb Trail for Taxonomy Pages
This example creates a breadcrumb trail for taxonomy pages. Use it in your theme’s taxonomy templates.
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>';
}
Display Term Parents Without Links
This example retrieves the term parents list without links.
$term_parents = get_term_parents_list($term_id, $taxonomy, array('link' => false));
echo $term_parents;
Display Term Parents Using Slugs
This example retrieves the term parents list with slugs instead of names.
$term_parents = get_term_parents_list($term_id, $taxonomy, array('format' => 'slug'));
echo $term_parents;
Display Term Parents With a Custom Separator
This example retrieves the term parents list using a custom separator.
$term_parents = get_term_parents_list($term_id, $taxonomy, array('separator' => ' > '));
echo $term_parents;