Hi

i try to display taxonomy term in node.tpl custom template...
i've tried so many ways but none work...

here an example :

print $node->taxonomy[1]->name

inspired by contemplate from another drupal install...so perhaps the number in [X] isnt right..what this number relative to ?

thanks for help, i gone mad

Comments

theabacus’s picture

X refers to the tid. If you want to display all the terms within a node you can...

foreach($node->taxonomy as $term) {
print $term->name;
}
aiphes’s picture

great it works, if i want to display a specific vocabulary ? is it possible ? how to that ? thx

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

theabacus’s picture

foreach($node->taxonomy as $term) {
if($term->vid=={my_vid}) {
print $term->name;
}
}

If you just print_r($node) you can see all the different values you have available..

nevets’s picture

Yes, add a check on vid, like this

// Change to the vocabulary id (vid) you want
$wanted_vid = 2;
foreach($node->taxonomy as $term) {
  if ( $wanted_vid == $term->vid ) {
    print $term->name;
  }
}
Anonymous’s picture

Here's another way, which I think would conform more to the way that we're supposed to do this stuff in D6

In template.php

themename_preprocess_node(&$vars) {
  $node = $vars['node'];
  $wanted_vid = 2;
  foreach($node->taxonomy as $term) {
    if ( $wanted_vid == $term->vid ) {
      $vars['my_terms'] .= $term->name; //You would need to format this the way you want it displayed, or pass it to a theme function
    }
  }
}

Then in your node.tpl.php, you can print the $my_terms variable where ever you need it.
I wrote this code from memory and haven't checked it for viability, but It's pretty close I think.

aiphes’s picture

ok i'll try it and give you feedback

thx

EDIT: great it's work very well..big thx

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

i need this code again for another project but i have a preprocess_node function in its template.php; how can i add this functionnality to my template.php ?

the actual code:


function cyrano_cg_preprocess_node(&$vars, $hook) {
//Partie regions dans node.tpl- ajoute les regions utiles au node.tpl
 $vars['pole_bloc_G'] = theme('blocks', 'pole_bloc_G');
 $vars['pole_bloc_C'] = theme('blocks', 'pole_bloc_C');
 $vars['pole_bloc_D'] = theme('blocks', 'pole_bloc_D');
 $vars['col_G1'] = theme('blocks', 'col_G1');
 $vars['col_G2'] = theme('blocks', 'col_G2');
 $vars['col_G3'] = theme('blocks', 'col_G3');
 //
//Partie template node.tpl
$node = $vars['node'];
$lesTypes=array('fiche_formation', 'page_pole','contenu_actualites');
//ajouter les vids possibles pour chaque quelquesoit le type
/* vid 1 pour pole formation
 * vid 2 pour évènement
 * vid 3 pour
 * vid 5 pour type actualite
 * vid 6 type de formation
 *
*/
$lesVid=array('1','6');
// on regarde si le type est dans le tableau
if ( in_array($node->type,$lesTypes) ) {
       if ( ! empty($node->taxonomy)  ) {
// Récupération du 1er element du tableau
           $term = array_shift($node->taxonomy);
    // verifie si l'un des termes appartiennent bien à l'un des vid définis dans le tableau
           if ( in_array($term->vid,$lesVid) ) {

              $tplfile = 'node-'.$node->type.'-'. $term->vid.'-'.$term->tid ;
              $vars['template_files'][] = $tplfile ;
          //drupal_set_message('Term name : '.$term->name,'status');
           drupal_set_message('Template file : '.$tplfile.'.tpl.php','status');
          }


      }
 //drupal_set_message('Type du node hors : '.$node->type,'status');
// drupal_set_message('Term name hors : '.$term->name,'status');
    }
    //$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
    //Pour afficher une seule taxonomie non cliquable 
  $node = $vars['node'];
  $vars['template_file'] = 'node-'. $node->nid;
  $wanted_vid = 3;//Choisir ici le vid diplome,
  foreach($node->taxonomy as $term) {
    if ( $wanted_vid == $term->vid ) {
      $vars['my_taxo_ficheform'] .= $term->name;
      //You would need to format this the way you want it displayed, or pass it to a theme function
      //Changer le nom de la variable si l'on ne se sert pas toujours du meme vid
    }
  }
}

?>

thanks

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

nevets’s picture

Take the code in cyrano_cg_preprocess_node() and add it to code in the existing hook_preprocess_node() function.

aiphes’s picture

the above code doesn't display anything...and

drupal_set_message('VID trouve : '.$term->vid.'Terme fiche formation :'.$my_taxo_ficheform,'status');

return nothing too..

what's wrong ?

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

ok after tweak it works like that :

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
    //Pour afficher une seule taxonomie non cliquable 
  $node = $vars['node'];
  $vars['template_file'] = 'node-'. $node->nid;
  $wanted_vid = 3;//Choisir ici le vid diplome,
  foreach($node->taxonomy as $term) {
    if ( $wanted_vid == $term->vid ) {
      $vars['my_taxo_ficheform'] .= $term->name;
       drupal_set_message('VID trouve : '.$term->vid.'Terme fiche formation :'.$my_taxo_ficheform,'status');
      //You would need to format this the way you want it displayed, or pass it to a theme function
      //Changer le nom de la variable si l'on ne se sert pas toujours du meme vid
    }
  }

how can i use this code for multiple distinct terms and content type ? if i create variable for each use like

$vars['my_taxo_use1'] .= $term->name;
$vars['my_taxo_use2'] .= $term->name;
$vars['my_taxo_use3'] .= $term->name;
$vars['my_taxo_use4'] .= $term->name;

is it good ? whithout modify the function, only add variables and vid..then i can put the variable in desired node-custom.tpl

how can i get multiple vid too?
thanks

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

aiphes’s picture

to have multiple vocabularies for multiple use, i need that

$wanted_vid

be an array..but this doesn't work :

$wanted_vid = array('2');

so how i can modify function to use it like i would ?

thanks

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data