views:

18

answers:

1

I want to be able to show or hide a text snippet on a node based on whether or not it was tagged with a specific taxonomy term.

Something along the lines of:

if (term('green')) {
  echo"this is green";
}
else {
  echo "not green";
}

What is the Drupal way of doing this?

A: 

Assuming from your example that you're working from inside of node.tpl.php, and that you have a single very specific term in mind:

You can access $taxonomy->taxonomy_term_YourTermID from within your node.tpl.php file, and test based on that.

If you'd like a more abstract solution, I'd recommend installing the devel module and building a function in your template.php files based on what shows up under the 'Devel' tab in the relevant node.

anschauung
$taxonomy->taxonomy_term_YourTermID does not seem to be working. Lets say it is tid '7'. How would I reference it?Tried $taxonomy->taxonomy_term_7. It does not work.
eGenius
Where are you adding the code? $taxonomy->taxonomy_term_7 should work in Drupal 6, in tpl.php files. But, it'll be a little different if you're adding the code in template.php or other places. Wherever you're working, adding a dpm(get_defined_vars()) should help you get a sense of what variables are available, if you have the devel module installed
anschauung
It also might depend on how you're testing: in the tpl.php files, $taxonomy->taxonomy_term_X returns an array('title' => 'foo_title', 'href' => 'foo_page', 'data' = array()) so simple if/then tests might not work as expected.
anschauung
I am most probably not using it properly. I have added this to my node-colors.tpl.php:<? if ($taxonomy->taxonomy_term_7): ?>this is green<? else: ?>not green<? endif; ?>Is this the right way to use it?
eGenius
Could you try adding a print('foo') and print('bar') statement to either side of that if/else block and let me know what the result is? I'm starting to suspect that your problem might be more basic
anschauung
Adding those snippets prints "foo" and "bar" before and after the if/else block.
eGenius
Thanks for your help. I got it working with the help of someone over at drupal.org (http://drupal.org/node/892612#comment-3368946).
eGenius

related questions