Last updated April 11, 2011. Created by chanakya on May 12, 2009.
Edited by olafveerman. Log in to edit this page.
Code below prints the terms in a particular vocabulary associated with the node.
You can just copy and paste the code in a block. Do not forget to change $vid value for the vocabulary.
To get $vid value for your vocabulary, just go to admin/content/taxonomy and mouse over the "edit vocabulary" link for the vocabulary you want to print. You can see vid at the status bar - the number which is at the end of the path.
<?php
/**
* Prints an unordered list of the terms (as links) that are
* associated to the currently displayed node.
*/
if ( arg(0) == 'node' && is_numeric(arg(1)) ) {
$node = node_load(arg(1));
$vid = 2;
$terms = taxonomy_node_get_terms_by_vocabulary($node, $vid);
if ($terms) {
print '<ul>';
foreach ($terms as $term) {
print '<li>'.l($term->name, 'taxonomy/term/'.$term->tid).'</li>';
}
print '</ul>';
}
}
?>
Comments
Great snippet but how would
Great snippet but how would one only call the first term instead of all terms.
Butler Raines, Atlanta Drupal Guy
Breaks the rest of content on the page
This snippet worked really good on one of my content types (locations) where I needed the info at the END of the node.
But now I want to print a term near the top of a node (the slug for an article). When I insert the code before the content, the term shows up, but the node doesn't print the rest of the content.
I'm not a PHP developer, but does anybody see how this code would break printing the rest of the content?
olafveerman, How would this
olafveerman,
How would this statement be written if you want to remove the list style ul and li classes. I tried myself but messed up the syntax.