Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

top level terms

By: bainternet on Aug 3rd, 2011  |  syntax: PHP  |  size: 1.26 KB  |  views: 171  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function get_term_top_most_parent($term_id, $taxonomy){
  2.     $parent = get_term_by( 'id', $term_id, $taxonomy);
  3.     while ($parent->parent != 0){
  4.         $parent = get_term_by( 'id', $term_id, $taxonomy);
  5.     }
  6.     return $parent;
  7. }
  8. // so once you have this function you can just loop over the results returned by wp_get_object_terms
  9.  
  10. function hey_top_parents($taxonomy) {
  11.                 global $post;
  12.         $terms = wp_get_object_terms(post->ID, $taxonomy);
  13.         $top_parent_terms = array();
  14.         foreach ($terms as $term){
  15.                 //get top level parent
  16.                 $top_parent = get_term_top_most_parent($term->ID, $taxonomy);
  17.                 //check if you have it in your array to only add it once
  18.                 if (!in_array($top_parent->ID,$top_parent_terms)){
  19.                         $top_parent_terms[] = $top_parent;
  20.                 }
  21.         }
  22.         foreach ($top_parent_terms as $term) {
  23.                         $r = '<a href="' . get_term_link($term->slug, $taxonomy) . '">' . $term->name . '</a>';
  24.                
  25.         }
  26.        
  27.         // return $r;
  28.        
  29.         return print_r($top_parent_terms);
  30.         // This shows an empty array (( [0] => )), so could  the problem be with the get_term_top_most_parent() function?
  31.  
  32. } // End of top parents
clone this paste RAW Paste Data