Replace Page Title with Image 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 &lt;h1&gt; 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.

&lt;?php $TitleImage = views_embed_view('title_image', 'block'); //Embed View ?&gt;

&lt;?php if (!empty($TitleImage)): //Check to see of View is empty ?&gt;
    &lt;h1 class="title" id="page-title"&gt;&lt;?php print $TitleImage; ?&gt; &lt;/h1&gt;
&lt;?php else: //Display regular title ?&gt;
    &lt;?php print render($title_prefix); ?&gt;
    &lt;?php if ($title): ?&gt;
        &lt;h1 class="title" id="page-title"&gt;&lt;?php print $title; ?&gt;&lt;/h1&gt;
    &lt;?php endif; ?&gt;
    &lt;?php print render($title_suffix); ?&gt;
&lt;?php endif; ?&gt;




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?

&lt;?php if (isset($node-&gt;field_title_image['und']['0']['filename'])): ?&gt;
&lt;h1 class="title" id="page-title"&gt;&lt;img src="/sites/default/files/images/&lt;?php print $node-&gt;field_title_image['und']['0']['filename']; ?&gt;" alt="&lt;?php print $node-&gt;field_title_image['und']['0']['alt']; ?&gt;"&gt;&lt;/h1&gt;
&lt;?php else: ?&gt;
&lt;?php print render($title_prefix); ?&gt;
&lt;?php if ($title): ?&gt;
    &lt;h1 class="title" id="page-title"&gt;&lt;?php print $title; ?&gt;&lt;/h1&gt;
&lt;?php endif; ?&gt;
&lt;?php print render($title_suffix); ?&gt;
&lt;?php endif; ?&gt;
To answer your specific question; clearly $TitleImage is not empty, views_embed_view is returning wrapper elements or a space. in the view settings make sure "no results" returns nothing using the no results behavior of the field and the view (under advanced).

Why use a view? If that image is on that piece of content then there is no need for a view at all, just use the field:

&lt;?php hide($content['field_title_image']); // So it does not render elsewhere ?&gt;
&lt;?php if (!empty($content['field_title_image'])): ?&gt;
  &lt;h1 class="title" id="page-title"&gt;
      &lt;?php print render($content['field_title_image']); ?&gt;
  &lt;/h1&gt;
&lt;?php else: //Display regular title ?&gt;
  &lt;?php print render($title_prefix); ?&gt;
  &lt;?php if ($title): ?&gt;
    &lt;h1 class="title" id="page-title"&gt;&lt;?php print $title; ?&gt;&lt;/h1&gt;
  &lt;?php endif; ?&gt;
  &lt;?php print render($title_suffix); ?&gt;
&lt;?php endif; ?&gt;


(if you are on node.tpl.php the filed will be as above, if you are on page.tpl.php it will be inside $page['content'] array)