So we have this:
if ( is_home() ) {
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/sample.jpg" /></div>';
}
elseif ( is_singular( array( 'post' ) ) ! has_post_thumbnail() ){
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
}
else ( is_singular( array( 'post', 'page' ) ) && has_post_thumbnail() ){
echo '<div id="featured-image">';
echo get_the_post_thumbnail($thumbnail->ID, 'header');
echo '</div>';
}
First things first, this if statement is not common and doesn’t make sense to most people:
elseif ( is_singular( array( 'post' ) ) ! has_post_thumbnail() ){
I read this and see:
if post is singular not has post thumbnail
Which would get me funny looks if I said it to someone out loud. Lets change it so it means what you actually want:
if post is singular and it does not have a post thumbnail
elseif ( is_singular( array( 'post' ) ) && ( ! has_post_thumbnail() ) ) {
You also have a syntax error, look at your else case, you’re doing this:
if ( condition ) {
// stuff
} else ( new condition ) {
// stuff
}
This is not valid PHP syntax, but even if it was, it’s unexpected, uncommon, difficult to catch, and violates every coding standard I’m aware of. You should instead use an elseif, so lets fix that
Also lets practice spartan programming and return as early as possible, removing all the else statements in the process:
if ( is_home() ) {
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/sample.jpg" /></div>';
return;
}
if ( is_singular( array( 'post' ) ) && ( !has_post_thumbnail() ) ){
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
return;
}
if ( is_singular( array( 'post', 'page' ) ) && has_post_thumbnail() ){
echo '<div id="featured-image">';
echo get_the_post_thumbnail($thumbnail->ID, 'header');
echo '</div>';
}
You finally have one more problem, not every function can be called outside the main loop, this includes has_post_thumbnail
. How does the function know which post you’re talking about if you haven’t set the current post yet? PHP isn’t psychic, so lets tell it:
global $post;
$post->ID;
Giving us:
global $post;
if ( is_home() ) {
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/sample.jpg" /></div>';
return;
}
if ( is_singular( array( 'post' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
return;
}
if ( is_singular( array( 'post', 'page' ) ) && has_post_thumbnail( $post->ID ) ){
echo '<div id="featured-image">';
echo get_the_post_thumbnail($thumbnail->ID, 'header');
echo '</div>';
}
Finally, look at your call to grab the thumbnail:
echo get_the_post_thumbnail($thumbnail->ID, 'header');
As I mentioned earlier, PHP isn’t psychic, and no amount of clairvoyance will fill the $thumbnail
variable with stuff, perhaps you mean the current post? If so use $post
instead.
Remember, you have to declare everything in advance. If you don’t tell the code about something and then try to use it, PHP will assume you mean null
or false
, or something similar, and generate a warning. This leads to all sorts of problems. Just because you know what you meant/intended doesn’t mean the computer does