There are multiple issues that I think you want to adjust:
- You open an extra PHP tag before the second
if
statement. That shouldn’t be there. - Make sure that
is_home()
is what you want. If you have a static front page (set in Settings > Reading), then you actually wantis_front_page()
. Think ofis_home()
asis_blog()
(to be clear,is_blog()
does not exist). - If you want to give
the_post_thumbnail()
a width and height, it needs to be in an array. Your other option is to define an image size in functions.php and then call that there. - I think your HTML can be cleaner by just always wrapping the banner in the #banner div.
- Just to be sure that you always have an image, I would check to make sure that you have a post thumbnail set and make sure you have a fallback.
With all those suggestions wrapped into code, here’s what I suggest:
In your functions.php add:
add_image_size( 'banner', 863, 328, true );
And wherever your banner snippet is:
<!-- Let's always wrap our banner in the banner div for easy styling -->
<div id="banner">
<?php
// Check to see if we're on a non-home-page and if the non-home-page has a featured image. use is_front_page() if that's desired
if( !is_home() && has_post_thumbnail() ) {
get_the_post_thumbnail( 'banner' );
// if we're on the home page OR don't have a featured image but DO have a dynamic sidebar
} elseif ( function_exists('dynamic_sidebar') && is_dynamic_sidebar('banner-widget') ) {
dynamic_sidebar('banner-widget');
// no featured image or on homepage with no sidebar, spit out a static image
} else {
echo '<img class="banner" src="' . get_template_directory_uri() . '/img/banner.jpg" alt="Banner 1">';
}
?>
UPDATE:
That final echo
statement was previously a complete mess. Sorry! I cleaned it up and I think it should work now. I also replated the bloginfo()
function (which is now deprecated) with the new, preferred equivalent function.