I've tried everything to get the tid of a taxonomy term in a node-nodeprofile.tpl.php template.

Using ConTemplate, I've found:

print $node->taxonomy[520]->tid

but that's not much use if I don't know the tid to start with.

print $node->taxonomy->tid

produces nothing at all.

I only have one vocabulary (vid 13) being used on nodeprofiles. I thought it would be quite a basic bit of php but haven't been able to find anything on drupal.org.

Comments

gausarts’s picture

Sorry just an ID number or names of the term ID?

If names, then try this.
1. Place this function in your template.php:

function yourtheme_taxonomy_links($node, $vid) {
  if (count($node->taxonomy)):
    $tags = array();
     foreach ($node->taxonomy as $term) {
       if ($term->vid == $vid):
          $tags[] = l($term->name, taxonomy_term_path($term));
          endif;
}
    if ($tags):
//Change ' ' to ' - ' or ' | ' or anything if you don't want an empty space between tags
	$output = implode(' ', $tags);
    endif;

  endif;

     if ($output):
       return $output;
     endif;

}

2. Call from your .tpl:

<?php print yourtheme_taxonomy_links($node, 13); ?>

where 13 is your vid

It's actually somewhere in google, but I forgot the link, sorry. Just try googling around for more info.

love, light n laughter
blogid.net

love, light n laughter

esllou’s picture

it was just the tid I needed as I need to use it in a link to a View.

archard’s picture

Why not just use the taxonomy_node_get_terms_by_vocabulary() function?

http://api.drupal.org/api/function/taxonomy_node_get_terms_by_vocabulary/5

Just do something like this

$term = taxonomy_node_get_terms_by_vocabulary($node->nid, {your vid})

The function returns an object, so you have to extract the tid from it.

$tid = $term->tid;

giorgio79’s picture

massimoi’s picture

I've tried
echo end($node->taxonomy)->tid; //Works if you have only one tid per node, so that end and first are the same

or

reset($node->taxonomy);
$k = key($node->taxonomy);
echo $node->taxonomy[$k]->tid;

--
Massimo - http://impronta48.it

xalexas’s picture

Put this in your node.tpl.php

$terms = taxonomy_node_get_terms($node);
foreach($terms as $term){

echo $term->tid;
}

or take a look here: http://drupal.org/node/44938