I'm developing my theme, and using <?php print $node->taxonomy[2]->name; ?> in node.tpl.php, but I wonder how to get random number, also not always 2, but it can change automatic and find by itself. Someone have good solution of this?

My print_r ($node) tell:
[taxonomy] => Array ( [2] => stdClass Object ( [tid] => 2 [vid] => 2 [name] => NYHETER / NEWS 2011 [description] => [weight] => 0 ) )

Or do you have a better solution either in template.php or node.tpl.php to get show ONLY name of taxonomy without a link. If you can in template.php, I prefer most and less coding in node.tpl.php :)

Comments

Kuldip Gohil’s picture

you can use following code to print taxonomy term name in node.tpl.php, you can get all term name associated with particular node.


$taxonomy = $node->taxonomy;

foreach($taxonomy as $key => $val){
$term[] = $val->name;
}

print implode(', ',$term);

Thank you,
Kuldip Gohil

truls1502’s picture

Thank you for your useful code, just wonder do you know how make it into template.php and make an variables where I can use in different node also, node-info.tpl.php and node-news.tpl.php etc?

Kuldip Gohil’s picture

in your template.php you can add code in following hook then variable will available in node.php


function YOURTHEME_preprocess_node(&$variables) {
$variables['my_taxonomy'] = "term1, term2"; //write your code here to get term associated with node
}

Now you can use $my_taxonomy in your node.tpl.php

Thank you,
Kuldip Gohil