3

I found how to do this if the node is tagged with certain terms here. But what I would like to do is to do something conditionally in node.tpl.php only if the node is tagged with a certain vocabulary.

For example, I have a vocabulary called "Color" with terms "Red" "Blue" "Yellow". I would like to display the text "It has color" only if the node is tagged with at least one of those terms. Otherwise print nothing.

How can I do this in node.tpl.php ?

1
  • are you using drupal 6 or 7?
    – gilzero
    Commented Jan 16, 2012 at 7:58

1 Answer 1

3

Each taxonomy term in a node also gives you the vocabulary id that it belongs to. So you could look through the terms to see if one matches the vocabulary id of your "Color" vocabulary and then display "It has color" when found.

An easy way to do this would be to use a loop:

<?php foreach ($node->taxonomy as $key => $term): ?>
  // Say the vocabulary id of "Color" is 10.
  <?php if ($term->vid == 10): ?>
    <div>
      <?php print views_embed_view('my-views', 'default', $args); ?>
    </div>
    // etc
    <?php break; ?>
  <?php endif; ?>
<?php endforeach; ?>

For reference, to access the vocabulary id of a single term:

$node->taxonomy[TERM_ID]->vid;

Where TERM_ID is the id of the taxonomy term.

2
  • Thank you @Laxman13. I actually need to print several <div> blocks including Views output inside. I simply thought I could do something like <?php if (!$title): ?> but obviously not.Sorry for my very limited php... For example if I want to print several of something like <div><?php print views_embed_view('my-views', 'default', $args); ?></div>, Should I print them inside php where "It has color" is in the example above? Or possibly I can use print<<<EOF ?
    – chinita7
    Commented Jan 16, 2012 at 15:46
  • Yes you can add something like that to where "It has color" is printed above. I changed the example in my answer to be more tpl friendly
    – Laxman13
    Commented Jan 16, 2012 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.