0

I am trying to build a node file for a content type in Drupal 7.59. I have for example a field for a taxonomy term. There is only 2 terms Expert and Guest. I want to display something on the node if this term reference choice is marked as Expert.

So I need to figure out how to write a line that checks for the value of the taxonomy term and then if it matches expert then it displays something but if it is guest it doesn't.

The field I have setup in my content type is:

  • Name of field: author type
  • Machine Name: field_author_type
  • Field Type: Term reference

The root taxonomy term is Author Type and the two terms are Expert and Guest. Does anyone know how to accomplish this? I hovered over the taxonomy term I wanted to reference and it was /taxonomy/term/57/ Is 57 the actual number or do I find it elsewhere?

So far I have this which is not much and it doesn't work so it is wrong. I am really sucky at php.

<?php if ($content['field_authors_type']->taxonomy[57]): ?>
    Put what you want to display here
<?php endif; ?>
1
  • So you're using a node template file, like node.tpl.php? If you want to check the value of a field, you can use something like this $content["field_authors_type"]["#items"][0]["tid"]. Sep 12, 2018 at 11:12

1 Answer 1

0

The value you're looking for would be stored like so

$content["field_authors_type"]["#items"][0]["tid"]

You could, if you wanted to be tidier, put this if statement in your theme's template.php in a template_preprocess_node or template_preprocess_page function (more on that here: https://api.drupal.org/api/drupal/modules%21node%21node.module/function/template_preprocess_node/7.x).

You would store your boolean value $variables['author_is_expert'] and then in your node.tpl.php, you can just use if ($author_is_expert)

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.