Hello,
I was wondering if anyone knew how to add the current taxonomy vocabulary to the node.tpl.php? I need any node under a vocabulary to display that vocabulary(no link is needed) in the node. I assume this is very simple but im still learning php and drupal.
thank you
Alex

Comments

ac’s picture

bump. Im sure this is really simple but if someone could help it would be greatly appreciated

styro’s picture

Don't forget that a node can have terms from multiple vocabularies. Based on a quick browse of the Drupal API, I don't think this is a one step problem unfortunately - corrections welcome from more experienced Drupal devs.

I'm just explaining how I'd go about figuring it out (I can't test any of this at the moment - sorry about the vagueness). Hopefully someone else could flesh it out with better code...

I notice node.tpl.php has a $taxonomy array (see http://drupal.org/node/11816). I'm not sure whether that is an array of ids, names or objects though. I have a horrible feeling it is already a themed list of links though.

If it is already a themed list of links, that approach probably wouldn't work that well, so going back a step I would start by getting an array of terms for that node via this function:
http://drupaldocs.org/api/4.6/function/taxonomy_node_get_terms

I would iterate through that array, and for each term I'd try to find out that terms vid from the term_data table. I'm not sure if there is an API function to do this directly without querying the database.

Here is an example function to get the vid (vocab id) from a tid (term id) - I pinched it from the taxonomy_context module:

function taxonomy_context_get_term_vocab($tid) {
  $vid = null;
  $result = db_query("SELECT vid FROM {term_data} WHERE tid = %d", $tid);
  while ($term = db_fetch_object($result)) {
    $vid = $term->vid;
  }
  return $vid;
}

Then once I had a vid, I'd get a vocabulary object using this function:
http://drupaldocs.org/api/4.6/function/taxonomy_get_vocabulary

From that vocabulary object, you could then get the name.

eg something like:

  $nodevocab = taxonomy_get_vocabulary($vid);
  $nodevocabname = $nodevocab->name;

Sorry about all the vague guesswork, and not being sure of how much PHP you know - I've probably just dropped you straight in the deep end :)

ac’s picture

Hey thanks. Good point about the multiple vocabs. I will take a look at that code, theres only one way to learn.

kae’s picture

thank you.

neubreed’s picture

I got it working in version 5 and it's based on the taxonomy_link() function. Seems to work

function get_node_vid_term_links($node,$vid) {
    $links = array();
    if (array_key_exists('taxonomy', $node)) {
        foreach ($node->taxonomy as $term) {
            if ($term->vid == $vid) {
                    $t_links['taxonomy_term_'. $term->tid] = array(
                        'title' => $term->name, 
                        'href' => taxonomy_term_path($term), 
                        'attributes' => array('rel' => 'tag' ,'title' => strip_tags($term->description)));
                }
        }
    }
    $links = theme('links', $t_links);
    return $links;
}
EduardoMercovich’s picture

Hello ac.

I am searching for the same answer. I have a few new flexinode types. Each one is categorized in 2 or 3 taxonomies, some of them shared.

As an example, a certain "technology" (the new flexinode) is related to one or more topics (agriculture, alternative energies, etc.), and one or more regions.

Now, al the topics and regiones appear together, but I would like them to show like:

Topics: topic A, topic E
Regions: region A, region C

Is this what you are searching for? I am sure that this scenario is common to many drupal installations.

Unfortunately, I am no coder nor do I know PHP... :-(

I hope you can find a solution for this. If my xhtml and css knowledge are useful, just drop me a note and I will be more that glad to help. :-)

Regards...

--
Eduardo Mercovich
Buenos Aires, Argentina

seanberto’s picture

Eduardo and AC,

Any answer yet? I'm looking to do the same thing and am not the best with PHP just yet.

I too am looking for a way to list both the vocabulary name, followed by a list of terms associated with a node.

I think this would be very useful, especially if the similar taxonomy term names appear in different vocabularies.

-sean

Sean Larkin

ericwagner’s picture

This isn't exactly what you are looking for, however, it might help. Basically, I needed to get the links/list of terms within individual vocabularies. Put the following into your theme's template.php file:

function get_node_vid_term_links($node,$vid) {
   $links = array();
   if (array_key_exists('taxonomy', $node)) {
     foreach ($node->taxonomy as $term) {
		if ($term->vid == $vid) {
		  $t_links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
		 }
     }
   }
   $links = theme('links', $t_links);
   return $links;
}

... and then place something like this in the node.tpl.php or flexinode equivalent:

<?php print get_node_vid_term_links($node,1) ?>

This would print the links for all of the terms for vocabulary 1. Just change the number for other vocabularies.

This doesn't exactly address what you are looking for, however, I hope it helps.

EduardoMercovich’s picture

It works just great Eric. :-)

Thanks a lot for your help. If someone happens to be in this same spot, I didn't had a template.php (I am yet using the default themes, so I created one that had only this code (just copy and paste into a template.php file and you are done).

<?php
function get_node_vid_term_links($node,$vid) {
$links = array();
if (array_key_exists('taxonomy', $node)) {
foreach ($node->taxonomy as $term) {
if ($term->vid == $vid) {
$t_links[] = l($term->name, taxonomy_term_path($term), array('rel' => 'tag', 'title' => strip_tags($term->description)));
}
}
}
$links = theme('links', $t_links);
return $links;
}
?>

Again, thanks for sharing your knowledge. :-)

Best regards...

--
Eduardo Mercovich
Buenos Aires, Argentina

summit’s picture

Subscribing, looking for code to pinpoint the exact term in a vocabulary also on node.tpl.php,
greetings, Martijn