I created a custom image field for a banner in one of my content type. I wish to display this banner in the page.tpl.php instead of node template. Here's my code in template.php:
function mytheme_preprocess_page(&$vars, $hook) {
$vars['banner'] = '';
if (!empty($vars['node'])) {
$node = $vars['node'];
if (isset($node->field_banner)) {
// Render the image as a variable in the page template
$vars['banner'] = theme_image_style(array('path' => $node->field_banner['und']['0']['uri'], 'style_name' => 'mybannerstyle', 'height' => '200', 'width' => '700'));
}
}
.....
and in the page.tpl.php, i coded:
<?php if(!empty($banner)){print $banner;} ?>
. This work perfectly in the node that has banner uploaded. But for nodes which have no banner, the following undefined messaged turn up:
Notice: Undefined index: und in mytheme_preprocess_page() (line 33 of /home/xxxxx/themes/mytheme/template.php).
I wonder how to solve this. Please advise. Thanks.