I'm attempting to replace the titles on my pages with images. I've added an image field to my content type and have created a view that displays the image for the selected page with a contextual filter.
I am embedding the view in my page.tpl.php page with views_embed_view()
.
I'm having a problem with the conditional statement. If no image exists for the selected page, I want to show the regular page title. Currently, my code will print an empty <h1>
tag on pages with no mages. Can I get some help with this?
I am using Drupal 7 and the Views module version 7.3.3.
<?php $TitleImage = views_embed_view('title_image', 'block'); //Embed View ?>
<?php if (!empty($TitleImage)): //Check to see of View is empty ?>
<h1 class="title" id="page-title"><?php print $TitleImage; ?> </h1>
<?php else: //Display regular title ?>
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title"><?php print $title; ?></h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php endif; ?>
I need to replace the page title in the page.tpl.php file. There are 3 content types that have the field_title_image field. I thought that creating a view with a contextual filter would be the best approach, to not display on content types that do not have the field_title_image field.
I came across this comment that helped me with displaying the image.
Below is my revised code. Is there a better solution?
<?php if (isset($node->field_title_image['und']['0']['filename'])): ?>
<h1 class="title" id="page-title"><img src="/sites/default/files/images/<?php print $node->field_title_image['und']['0']['filename']; ?>" alt="<?php print $node->field_title_image['und']['0']['alt']; ?>"></h1>
<?php else: ?>
<?php print render($title_prefix); ?>
<?php if ($title): ?>
<h1 class="title" id="page-title"><?php print $title; ?></h1>
<?php endif; ?>
<?php print render($title_suffix); ?>
<?php endif; ?>