Adding conditions to the template file of a view I'm setting up a webshop in drupal and I bought a template.

The tempate has a compare products function embedded, that works with a button on the product pages, however, I would only like that button to show up at certain types of products, like TVs and phones etc., but not for the rest. 

The compare button, however, is hardcoded into the template file and not manageable from views, so I have to add the conditions to it there, in PHP - to only appear on the node pages of products that have a certain taxonomy term attached to them.

 &lt;div class="actions"&gt;
    &lt;?php print flag_create_link('wishlist', $node-&gt;nid); ?&gt;
    &lt;?php print flag_create_link('compare', $node-&gt;nid); ?&gt;
  &lt;/div&gt;&lt;!-- .actions --&gt;
&lt;/div&gt;




This is the section in the code of the node--product.tpl.php file that puts out the action buttons. I need the second one, the compare button to only appear for nodes that have a certain taxonomy term. 

I don't really know php, except for very very basic syntax, so I would be really grateful, if someone could describe in detail how to implement a solution, perhaps even provide a snippet of code that I can customize and paste there!

Thank you in advance!

Huba
To add a variable in template files you first need to create it in your template.php of the theme folder using template_preprocess_node function.



/**
* Implements template_preprocess_node.
*/
function theme_preprocess_node(&$variables){
  foreach($variables['field_taxonomy_field'] as $values){  //field_taxonomy_field is the machine name of the taxonomy term field of your content type.
    if($values['tid'] == $term_id){  //$term_id if the id of the term you want to compare. 
      $variables['compare_products'] = l("Compare Button", 'node/compare');
      //@todo your stuff here.
    }
  }
}



This will create a variable named $compare_products which you can use in the template file as a variable.

In node--product.tpl.php you can add &lt;?php print $compare_products; ?&gt; and the data from the variable will be shown whenever the variable is available.