Community & Support

Best way to display taxonomy in a custom content.type.tpl file?

I've got a pretty good question concerning Taxonomy in Drupal when using a custom content.tpl.php file.

What is the most efficient way to get taxonomy terms associated with your node when wanting to break them apart? Keeping in mind Queries and security.

I'm currently using the module "Content Taxonomy" in order to get rid of the Taxonomy fields on the form, and with the being said this module also gives a new field for your specified taxonomy term.

For example sake Vocab " Fruits", can become field_fruits in CCK.

What is better? For people who want to break apart taxonomy, instead of a comma separated list.

The "1" below is vocab I.D 1.

$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 1);
foreach ($terms as $term) {
  $tags[] = l($term->name, taxonomy_term_path($term));
    }
print t("Fruits") . ": " . implode(' | ', $tags);

or this,( the Content taxonomy field produced)

print $node->field_category_fruits[0]['view']; ?>

Comments

The second if it meets your needs

The second if it meets your needs since the queries have already been done. You can also use a modified version of the first snippet that avoids querying the database like this

$tags = array();
foreach ($node->taxonomy as $term) {
  if ( $term->vid == 1 ) {
    $tags[] = l($term->name, taxonomy_term_path($term));
  }
}
print t("Fruits") . ": " . implode(' | ', $tags);

Very cool nevets. I never

Very cool nevets. I never thought about that, and thankfully I use no hierarchies.

So, let me pick your brain 1 more time to take this a step further.

Would using your snippet and getting rid of the Content taxonomy module altogether be a wise move? Or are you not familiar with the module?

Thank you very much for your detailed response nevets!

Sorry, I have not used the content taxonomy module

So I have no idea what it's pro's and con's are.

In what type of scenario

In what type of scenario would someone want to use taxonomy_get_terms_by_vocabulary in their content-tpl.php file rather than the snippet listed? Or better yet, what is the difference in the 2 snippets?

$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 1);
foreach ($terms as $term) {
  $tags[] = l($term->name, taxonomy_term_path($term));
    }
print t("Fruits") . ": " . implode(' | ', $tags);

$tags = array();
foreach ($node->taxonomy as $term) {
  if ( $term->vid == 1 ) {
    $tags[] = l($term->name, taxonomy_term_path($term));
  }
}
print t("Fruits") . ": " . implode(' | ', $tags);

This would be for anyone trying to associate and break apart vocab with Nodes in their custom.tpl.php file.

The first requires a database query

The first requires a database query (the one that calls taxonomy_node_get_terms_by_vocabulary()), the second a loop to find the desired terms (the second one relies on the fact the taxonomy module has already queried the database for all the terms the node has used. The second case can also be generalized like this

$tags = array();
$vocabs = taxonomy_get_vocabularies($node->type = NULL);

foreach ((array)$node->taxonomy as $term) {
    $tags[$term->vid][] = l($term->name, taxonomy_term_path($term));
}
foreach ( $tags as $vid => $links ) {
  print '<div>' . $vocabs[$vid]->name . ": " . implode(' | ', $links) . '</div>';
}

For anyone wanting to

For anyone wanting to display the Vocabulary name and the terms associated with that vocabulary, the generalized snippet Nevets posted above this post works perfect. . We should start a fund for VeryMisunderstood and Nevets for all their help! Thanks Nevets! I'm going to create a book page or submit a snippet with these instructions .

The only question now would be the correct way to use the first snippet for 2 different vocabs. Basically, if you had Vocab Fruits and Vocab Vegatables

Would it be ..

<?php
$tags
= array();
foreach (
$node->taxonomy as $term) {
  if (
$term->vid == 1 ) {
   
$tags[] = l($term->name, taxonomy_term_path($term));
  }
}
print
t("Fruits") . ": " . implode(' | ', $tags);
?>

<?php
$tags
= array();
foreach (
$node->taxonomy as $term) {
  if (
$term->vid == 2 ) {
   
$tags[] = l($term->name, taxonomy_term_path($term));
  }
}
print
t("Vegetables") . ": " . implode(' | ', $tags);
?>

or can you combine theme?

That will work as is

Note if you name your vocabularies properly (in your case Fruits and Vegetable) the general script handles that by default.

And while it will work, one loop would be more efficent, so you could code it like

<?php
$tags
= array();
foreach (
$node->taxonomy as $term) {
 
$tags[$term->vid][] = l($term->name, taxonomy_term_path($term));
}
print
t("Fruits") . ": " . implode(' | ', $tags[1]);
print
t("Vegetables") . ": " . implode(' | ', $tags[2]);
?>

Note this should look alot like the generalized code only instead of a loop to handle printing it calls print for each vocabulary you want to print.

A friend suggested this

A friend suggested this Snippit. What do you think about this Nevets? Or anyone familiar?

For Template.php

<?php
function mythemename_taxonomy_links($node, $vid) {

//if the current node has taxonomy terms, get them
 
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):
//get the vocabulary name and name it $name
       
$vocab = taxonomy_get_vocabulary($vid);
       
$name = $vocab->name;
       
$output .= t("$name") . ": " . implode(' | ', $tags);
    endif;

  endif;

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

}
?>

For Node.tpl.php , Note: 1 is the vid for the vocabulary I want to call.

<?php print mythemename_taxonomy_links($node, 1); ?>

What do you think about separating taxonomy this way instead?

Nicely packaged

It is a nice way to package it, my only concern would be if you were to call mythemename_taxonomy_links more than once since it requires looping through $node->taxonomy more than once. The issue of performance comes into play when $node->taxonomy has a lot of terms and/or you call mythemename_taxonomy_links many times. (I try not to walk the same array twice in the same code).

Thanks for your feedback,

Thanks for your feedback, and your response makes sense. There really isn't much documentation about doing thing, and with what does exist..a lot is from Drupal 4.7.

The average person won't have more than 3-4 different vocabularies, with that in mind (forgetting about using Content taxonomy)
What is the best way to break apart Taxonomy?

1.) If no more than 2 or 3, vocabs, would you suggest your initial snippet?

2.) Or perhaps, the one I posted above?

3.) Or using a CCK Taxonomy module, and printing out the taxonomy using cck? Like my first posted showed?

Thanks, this should be my last question regarding this issue :)

It depends on your goal

If you only want to print some of the terms this way (that is the node collects terms from N vocabularies and you want to print less than N vocabularies this way) I would use the snippet above since it does error checking (my initial snippet made some assumptions).

If you want to print all the vocabulary terms for the node this way I would use my 2nd snippet since it is more compact and handles the case of no terms.

(I can not really commnt on the CCK taxonomy module as I have not used it).

subscribing

chris

http://SocialNicheGuru.com
Creating gurus to deliver the right product and the right message to the right niche at the right time

Subscribing, interested in

Subscribing, interested in resulting snippet
Greetings,
Martijn

vocabulary and taxonomy terms

Hi!
I had the following code in Drupal 5.x:

<?php $vocabs = array(); foreach( (array)taxonomy_node_get_terms_by_vocabulary($nid, 3, $key = 'tid') as $term ) { $vocab = taxonomy_get_vocabulary($term->vid);
if ( !isset(
$vocabs[$vocab->name]) ) { $vocabs[$vocab->name] = array(); } $vocabs[$vocab->name][] = l($term->name, "taxonomy/term/$term->tid"); }
foreach (
$vocabs as $name => $links ) { print '<div><em>' . $name . ':</em> '; print implode(', ', $links); print '</div>'; } ?>

Its role was to write the third term into the node.tpl.php including the name of the category.
I updated my site to Drupal 6.x and this code doesn’t work.
I was looking for a solution for 6.x but haven’t found, yet. google://drupal+terms+by+vocabulary

I found solutions where all the categories were listed by name but I didn’t find a solution where I can get listed the categories I want by node.tpl.php.
The code above might not be the best but it worked in Drupal 5.x
Looking forward to your answer,
Viki

nobody click here