Advertisement
bainternet

top level terms

Aug 3rd, 2011
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement