How to display taxonomy terms from an individual vocabulary separately in Drupal
By default drupal prints out all terms from all vocabularies in the same place. Sometimes this is fine, but there are definitely occasions where it is preferable to have control over where each vocabulary is displayed on and individual basis. Fortunately Drupal can be made to do just about anything if you understand how it works. Below is a function I wrote that allows you to isolate a particular vocabulary for display where ever you want. The code below should be pasted into your template.php file.
<?php
/* This function accepts nid, vid and a string "name" which is used to print out the terms of an individual vocabulary*/
function individual_vocabulary_list($nid, $vid, $name) {
$terms = taxonomy_node_get_terms_by_vocabulary($nid, $vid);
if ($terms) {
$links = array();
$output .= t($name) . $vocabulary->name . ': ';
foreach ($terms as $term) {
$links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
}
$output .= implode(', ', $links);
}
return $output;
}
?>
Then where ever you want to print out the vocabulary in your theme place the following code:
<?php individual_vocabulary_list($node->nid, 5, 'Location');?>
The 5 in this example refers to the vid I wish to access. You will need to change it to match the vocabulary for your site. The string "Location" is used to change the label for each vocabulary section that is to be displayed.




Post new comment