1

I added a custom field in the ‘article‘ content type.
I am using bootstrap as a theme.
I want to print that field value as a css class in the h1 element of every node of type : article

So I duplicated the page.tpl.php in my subtheme template folder and changed the h1 code from :

<h1 class="page-header"><?php print $title; ?></h1>

to

<h1 class="page-header <?php print $node->field_custom_field['und'][0]['value']; ?>"><?php print $title; ?></h1>

The problem is that sometimes sometimes I get this error message

    Notice: Undefined property: stdClass::$field_custom_field in include()  
(line 157 of /home/frost/sites/all/themes/subtheme/templates/page.tpl.php).

I have basic php knowledge, so my guess is I need to add a condition saying that the custom field is only printed if the template file is called when an article content type node is being viewed.

How can I add this condition in the template file ?

Thank you

1 Answer 1

1

Duplicate node.tpl.php and rename it node--article.tpl.php and then it's just:

print render($content['field_custom_field']);

If you really want to stay on the page level you better use page--node--article.php and then you need to get hold of the $node object first somehow. Maybe take a look at Clive's answer here: https://drupal.stackexchange.com/a/32310/15055

1
  • Thank you for your answer. I have been able to add the field value as a class within the node--article.tpl.php it works fine using <?php print $node->field_custom_field['und'][0]['value']; ?> , i'm trying to get it on the page level too because the h1 element is in the page.tpl.php. The node tpl file only outputs the actual node content. Everything else, Metadata, Title etc.. is in the page tpl file. That's why i'm looking for a way to print only if the page tpl is called when a article type node is being viewed. Thanks again !
    – NSTK
    May 6, 2018 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.