get_term_ancestors()

This WordPress function requires the CPT-onomy class.

You have been redirected from rachelcarden.com to my new home at WP Dreamer! I moved all my content over so you should be able to find everything you're looking for. If not, please let me know!

Description

Merges all term ancestors into a single array of their IDs.

This recursive function will merge all of the ancestors of the term into the same array of term IDs, from lowest to highest in the hierarchy. Only useful for taxonomies which are hierarchical.

Usage

 $cpt_onomy->get_term_ancestors( int $term_id, string $cpt_onomy );

Parameters

$term_id (integer) (optional)
Default: none
The term's ID.

$cpt_onomy (string) (required)
Default: none
Name of the CPT-onomy that the term belongs to.

Return Values

(array|WP_Error) List of Term IDs. WP_Error returned if $cpt_onomy does not exist.

Examples

Get an array of ancestor terms and display them, with links, in an unordered list.

<?php

global $cpt_onomy;
$taxonomy = 'actors';
$term_ancestors = $cpt_onomy->get_term_ancestors( 10, $taxonomy );

echo '<ul>';
foreach ( $term_ancestors as $ancestor ) {
   $term = $cpt_onomy->get_term_by( 'id', $ancestor, $taxonomy );
   echo '<li><a href="' . $cpt_onomy->get_term_link( $term, $taxonomy ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';

?>

Notes

  • Added in version 1.1.

Related Functions

, , , , ,