1

I have this function that I used in a similar situation (Drupal show all terms of different vocabulary) that prints the terms of a node with a corresponding parent (if applicable). In my template.php file:

function theme479_print_terms($node, $vid = NULL, $ordered_list = TRUE) {
     $vocabularies = taxonomy_get_vocabularies();
     if ($ordered_list) $output .= '<ul>'; //checks to see if you want an ordered list
     if ($vid) { //checks to see if you've passed a number with vid, prints just that vid
        $output = '<div class="tags-'. $vid . '">';
        foreach($vocabularies as $vocabulary) {
         if ($vocabulary->vid == $vid) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
           if ($terms) {
             $links = array();
             $output .= '<span class="only-vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
              // Check for terms parent, if found assign to $parent variable
              if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) {
                foreach ($get_parents as $parents) {
                  $parent = $parents->name . '/';
                }
              }
               $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</span>';
           }
         }
       }
     }
     else {
       $output = '<div class="tags">';
       foreach($vocabularies as $vocabulary) {
         if ($vocabularies) {
           $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabulary->vid);
           if ($terms) {
             $links = array();
             $output .= '<ul class="vocabulary-'. $vocabulary->vid . '">';
             if ($ordered_list) $output .= '<li class="vocabulary-'. $vocabulary->vid . '">' . $vocabulary->name . ': ';
             foreach ($terms as $term) {
              // Check for terms parent, if found assign to $parent variable
              if ($get_parents = taxonomy_get_parents($term->tid, 'tid')) {
                foreach ($get_parents as $parents) {
                  $parent = $parents->name . '/';
                }
              }
               $links[] = '<span class="term-' . $term->tid . '">' . l($parent . $term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description))) .'</span>';
             }
             $output .= implode(', ', $links);
             if ($ordered_list) $output .= '</li>';
             $output .= '</ul>';
           }
         }
       }
     }
     if ($ordered_list) $output .= '</ul>';
     $output .= '</div>';
     return $output;
}

I have this vocabulary structure:

  • Foods
    • Fruits
    • Fruit_with_seeds

This function only shows:

  • Fruits
    • Fruit_with_seeds

Is it possible to show the entire hierarchy of a vocabulary?

2

I'm not sure what exactly you are looking for. The taxonomy terms for a node are normally nested inside $node->taxonomy. If you want all the terms "above" the terms inside the node, you should take a look at taxonomy_get_tree(). To the code above: I would urge you to seperate the tree-logic from the output; first create the array, then render the output.

0
$result = db_query('SELECT DISTINCT(tn.nid), t.tid, t.name, n.nid, n.title 
FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid 
INNER JOIN {term_data} t ON tn.tid = t.tid ORDER BY n.nid DESC');

while ($o = db_fetch_object($result)) {
  $items[$o->tid][] = l($o->title, 'node/' . $o->nid);
  $terms[$o->tid] = $o->name;
}

if (isset($items)) {
  foreach ($items as $tid => $item) {
    echo theme('item_list', $item, $terms[$tid]);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.