Change TPL file based on taxonomy term I would like to change the tpl.php file used depending on the taxonomy term applied to a node.

There is a snippet on this page that explains how to do that. However, it is written for Drupal 6, and doesn't work for Drupal 7.

The snippet is as follows:

// Add this to your theme's template.php
// Replace "THEMENAME" with your theme's name
function THEMENAME_preprocess_node(&amp;$vars) {
  foreach ($vars['node']-&gt;taxonomy as $term) {
    $vars['template_files'][] = 'node-term-'. $term-&gt;tid;
  }
}


I have tried to update it for Drupal 7 as follows:

function THEMENAME_preprocess_node(&amp;$variables) {
  foreach ($variables['node']-&gt;taxonomy as $term) {
    $variables['template_files'][] = 'node-term-'. $term-&gt;tid;
  }
}


However, it still doesn't work. I just get the following error message:


  Notice: Undefined property: stdClass::$taxonomy in
  THEMENAME_preprocess_node()  Warning: Invalid argument supplied for
  foreach() in THEMENAME_preprocess_node()


Any help would be appreciated.
You could probably handle it with the following code:

function mytheme_preprocess_node(&amp;$variables) {
  if (isset($variables['node']-&gt;field_myfield[$variables['node']-&gt;language][0]['tid'])) {
    $tid = $variables['node']-&gt;field_myfield[$variables['node']-&gt;language][0]['tid'];
    $variables['theme_hook_suggestions'][] = 'node__term_'. $tid;
  }
}


Now if a term with the id 6 would be referenced from a node's 'field_myfield' Drupal would look for a template named 'node--term_6.tpl.php' and use that if available.