Printing Node Variables in page.tpl.php

Public

Preprocess Function to Make Fields Available to Node Templates: Credit from http://josephfitzsimmons.com/creating-a-hero-page-template-in-drupal/

</> CopyGet raw version
php
  1. function MYTHEME_preprocess_page(&$variables) {
  2.  
  3. // Check to see if a hero image field is set
  4. if (isset($variables['node']->field_header_image[LANGUAGE_NONE][0])) {
  5.  
  6. // Store the image into $img for easy reference
  7. $img = $variables['node']->field_header_image[LANGUAGE_NONE][0];
  8.  
  9. // Build the $hero array to be used in templates/page/page--hero.tpl.php
  10. $variables['hero']['path'] = image_style_url('landing_page_hero_image', $img['uri']);
  11. $variables['hero']['alt'] = $img['alt'];
  12. $variables['hero']['title'] = $img['title'];
  13.  
  14. // Add this line if you only want the variables available on this specific template. Add a theme hook suggestion to load templates/page/page--hero.tpl.php
  15. $variables['theme_hook_suggestions'][] = 'page__hero';
  16. }
  17. }

page.tpl.php - Print Variables IF the Field Exists

</> CopyGet raw version
php
  1. <?php if (isset($hero)): ?>
  2. <img src="<?php print $hero; ?>">
  3. <?php endif; ?>