I'm using contemplates to display certain fields based on what taxonomy terms a node has. I'm a newbie at php so please point out if I've got any bad code.

I tried using:

if(isset($node->taxonomy[$term_id])) {
      return true;
}

and...

return !is_null($node->taxonomy[$term_id]);

Neither seems to work. I understand that $taxonomy is an array of objects, so all I need to do is check if an object of $tid exists in it. Perhaps I need to brush up my php?

Thanks in advance,
Ben

Comments

WorldFallz’s picture

I think what you're looking for is the in_array() php function.

===
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend." - Worldfallz

keyo’s picture

I tried that. Doesn't look like I can compare two objects?

return in_array(taxonomy_get_term(5),$node->taxonomy);
nevets’s picture

If you are in a template and not a function, normally you want to print something (not return). Lacking the context for the code above this may or may not help but unless that code is in a function that is used to test the truth of something it does not look like it will do anything is useful (from a templating point of view).

keyo’s picture

The content type I am using the template for is a classified listing. Listings get different terms (free, premium, basic. etc.) by the Pay2Publish module. Fields are printed based on what terms are attached to the node. e.g. The video field only gets shown on premium listings.
So to make my code read better I made a function _is_premium() and used it like this...

if(_is_premium or _is_basic()) {
  print $content->field_name['view'];
}

Since the content type has many fields I want to reuse code as much as I can. I hope that puts it into context.

nevets’s picture

You need to pass $content to the function, so the call to _is_premium becomes _is_premium($content) and the function would be declared like

function _is_premium($content) {
  ...
}