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.

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

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!


1 Answer 1


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 <?php print $compare_products; ?> and the data from the variable will be shown whenever the variable is available.

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.