Skip to content

Instantly share code, notes, and snippets.

@ericpedia
Created August 4, 2011 16:30
Show Gist options
  • Save ericpedia/1125575 to your computer and use it in GitHub Desktop.
Save ericpedia/1125575 to your computer and use it in GitHub Desktop.
Get top-most parent of a taxonomy term
<?php
/****************************************************
Get top-most parent of a taxonomy term
****************************************************/
function get_term_top_most_parent($term_id, $taxonomy){
$term = get_term_by( 'id', $term_id, $taxonomy);
if ( $term->parent != 0 ) {
return get_term_top_most_parent ( $term->parent, $taxonomy );
} else {
return $term;
}
}
function hey_top_parents($taxonomy) {
$terms = wp_get_object_terms(get_the_ID(), $taxonomy);
$top_parent_terms = array();
foreach ($terms as $term){
//get top level parent
$top_parent = get_term_top_most_parent($term->ID, $taxonomy);
//check if you have it in your array to only add it once
if (!in_array($top_parent->ID,$top_parent_terms)){
$top_parent_terms[] = $top_parent->ID;
}
}
foreach ($top_parent_terms as $term) {
$r = '<a href="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</a>';
}
return $r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment