get_term_parents_list

The timeline below displays how wordpress function get_term_parents_list has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.5

/**
 * Retrieves term parents with separator.
 *
 * @since 4.8.0
 *
 * @param int          $term_id  Term ID.
 * @param string       $taxonomy Taxonomy name.
 * @param string|array $args {
 *     Array of optional arguments.
 *
 *     @type string $format    Use term names or slugs for display. Accepts 'name' or 'slug'.
 *                             Default 'name'.
 *     @type string $separator Separator for between the terms. Default '/'.
 *     @type bool   $link      Whether to format as a link. Default true.
 *     @type bool   $inclusive Include the term to get the parents for. Default true.
 * }
 * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
 */
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;
}

WordPress Version: 4.8

/**
 * Retrieve term parents with separator.
 *
 * @since 4.8.0
 *
 * @param int     $term_id  Term ID.
 * @param string  $taxonomy Taxonomy name.
 * @param string|array $args {
 *     Array of optional arguments.
 *
 *     @type string $format    Use term names or slugs for display. Accepts 'name' or 'slug'.
 *                             Default 'name'.
 *     @type string $separator Separator for between the terms. Default '/'.
 *     @type bool   $link      Whether to format as a link. Default true.
 *     @type bool   $inclusive Include the term to get the parents for. Default true.
 * }
 * @return string|WP_Error A list of term parents on success, WP_Error or empty string on failure.
 */
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;
}