On my website, I have used the Taxonomy to created different sections. Each section has its own "contact us" page. I want to create a variable that will create a path to the relevant contact page. That way I can print the variable in taxonomy-term.tpl.php, node.tpl.php and page.tpl.php files to print the path dynamically.

How can I test for the taxonomy, and then change the value of the variable accordingly?

I've looked around for an answer and the closest I could find was How to move custom code into template.php. However, it requires a taxonomy reference field to present to perform the test and so wont work on the main taxonomy index pages (which cannot have taxonomy field references).

EDIT: Please note: The contact us page is just a normal node with telephone numbers on it. It is not the Drupal contact us form.

1 Answer 1


There is a Drupal Dojo video that answers this exact question. It is done in Drupal 6 but might give you some clues how to accomplish this in Drupal 7.

(edit: Response to comment) Although it might add complexity to developing your website, you can use the Context module to trigger block visibility based on certain criteria such as a View being viewed or a taxonomy term page being viewed. Keep in mind that if the Context module is used with all blocks disabled in the block UI it gives a performance boost to the website.

  • EDIT: Please note: The contact us page is just a normal node with telephone numbers on it. It is not the Drupal contact us form. Commented Jun 17, 2011 at 19:57
  • Isn't that overkill? Surely there must be a way to just test for taxonomy within template.php. Commented Jun 18, 2011 at 10:10
  • 1) Context module is said to perform better than blocks. 2) You want to control elements based on context so it's appropriate to use the Context module 3) If you know how to work with PHP try using HOOK_page_alter() in the template.php file to find the variables to create conditions and to change others.
    – Adam S
    Commented Jun 18, 2011 at 10:32
  • Thanks for the reply! Do you have any links to resources that show how to use hook_page_alter() to test for taxonomy? Thanks again! Commented Jun 18, 2011 at 10:40
  • Basically Drupal 7 creates one massive array, let's the developer alter the array, then recursively works through the array wrapping all the data in HTML. Here is a good video on the new render array.
    – Adam S
    Commented Jun 18, 2011 at 11:21

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.