I know this is more of a theming inquiry, but the theme I'm using isn't widely distributed and I wanted to expand the pool of knowledge to solve this. Right now, I have a two page backgrounds, one for the main page and one for content pages. It is pulled like so in my page.tpl.php:

<?php if($is_front): ?>
                <img id="main-image" src="<?php print $base_path . $directory; ?>/images/home-bg.jpg" alt="<?php print $site_name; ?>" />
              <?php else: ?>
                <img id="content-image" src="<?php print $base_path . $directory; ?>/images/content-bg.jpg" alt="<?php print $site_name; ?>" />
              <?php endif; ?>

What I would like to do, if it is possible, is create a field called field_bgimage assign it to all content types. Then have the page.tpl.php file check the current node to see if the field_bgimage is populated and if so, display that image as the page background instead of the two fallback images currently reflected in the code above. Is it even possible to have the page template pull and use a node's field?

Comments

Gilles G.’s picture

Hello SuperRookie,

I've the same question on my post today : http://drupal.org/node/1260426. Any idea ?

:)

SuperRookie’s picture

Okay, I got this working with the help of a few other people. With the following in my page preprocess:

<?php
function mytheme_preprocess_html(&$vars) {
  $node = menu_get_object();
  if ($node && $node->nid > 0) {
    if (isset($node->field_bgimage['und'][0])) { // Or whatever your field is called
      $vars['image'] = theme('image', array('path' => $node->field_bgimage['und'][0]['filepath'], 'alt' => 'Alt text', 'attributes' => array('id' => 'my-id')));
    }
  }
}
?>

And this in my page template where it calls the background images:

<?php if($is_front): ?>
                <img id="main-image" src="<?php print $base_path . $directory; ?>/images/home-bg.jpg" alt="<?php print $site_name; ?>" />
              <?php else: ?>

<!-- background --><?php if (!empty($node->field_bgimage['und'][0]['filename'])): ?><?php echo theme('image', array('path' => file_build_uri('./' . $node->field_bgimage['und'][0]['filename']), 'alt' => 'Background Image')); ?>

<?php else: ?>
<img id="content-image" src="<?php print $base_path . $directory; ?>/images/content-bg.jpg" alt="<?php print $site_name; ?>" />
<?php endif; ?><!-- /#background -->

              <?php endif; ?>

It returns the main page image if it's on the main page, and then the content's bgimage field as the bg unless none is provided, then it falls back on content-bg. Works perfectly!

SR

dddave’s picture

Status: Active » Fixed

"Time" seems to have fixed this. Closing old, stale issues. If the issue still persists feel free to re-open.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.