0

I have a taxonomy term called 'price'. I'd like it to display on the node page if it exists. But I don't want the taxonomy value to appear on the node page, I want the label to appear 'Price:' followed by hard-coded text that says 'see website.'.

Is this possible, and if so, how?

I don't want to use String Overrides because I'm using the value for the taxonomy term elsewhere. Perhaps some sort of code-based method (no pun intended) would work better :)

1 Answer 1

1

You can override the display your theme node.tpl.php template. Place something like this in your template.php...

function MYTHEME_preprocess_node(&$variables) {
   $variables['price_display'] = '';
   if(isset($variables['node']->field_terms[LANGUAGE_NONE]) && is_array(($variables['node']->field_terms[LANGUAGE_NONE]))) {
     foreach($variables['node']->field_terms[LANGUAGE_NONE] as $tag) {
        if(isset($tag['taxonomy_term']) && $tag['taxonomy_term']->name == 'price') {
          $variables['price_display'] = 'see website';
          break;
        }
      }
    }
  }
}

At this point you should have the variable $price_display available to print out in node.tpl.php.

3
  • thanks. The logic seems intelligible. But I can't see that deep into the node variable to see the field_terms you're referencing. Can you explain your code to me? This code as is (replacing price_display with field_price) gives no errors but seems to change nothing. When I dpm() I can't see into the node variable you're referencing. The only variable I can see that needs changing is $vars['field_price'][0]['taxonomy_term']->name...not sure where to go from here. Thanks again.
    – Sage
    Commented Apr 23, 2015 at 2:14
  • Sure, the nested if/foreach/if is just making sure there's a node object with a field property array it can loop through to look for taxonomy objects with the term name "price". There's probably a better way to do that I'm just going off the idea that there are many node types that may not have the same structure (thus many different structure checks).
    – Shawn Conn
    Commented Apr 23, 2015 at 2:52
  • If you can dpm() your way into the node var, you should be able to figure it out what you're looking for inside the taxonomy field you're using. Once you've done that you can set $variables['price_display'] so you can reference it in your node template.
    – Shawn Conn
    Commented Apr 23, 2015 at 2:55

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.