0

I'm creating a website and have inserted a blog, in my if statement the home page has a specific image to use and then of course my pages will use the featured image I choose per page. I'd like to make it so if I don't add a featured image to my blog posts it defaults to a specific one.

So it would go, if it does not have has_post_thumbnail then use "images/blog-banner.jpg". At the same time, if there IS a has_post_thumbnail present, I'd like to still use that one.

I've tried to do it myself but I've always been confused with the if statement..

/** Add the featured image section */
add_action( 'genesis_before_header', 'minimum_featured_image' );
function minimum_featured_image() {
    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>';
    }
}

Any advice would be great!

3

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

  • Thanks so much for the answer. Sorry it took so long! I do have another thought. I have a logo on a page that I'd like to be centered if there is no featured image. Is there some thing I can do with this if else to have it do that if there IS a featured image the logo is positioned to the left, but if there ISNT the logo will be centered? – kia4567 Apr 6 '15 at 18:49
  • You could show a logo and use CSS to centre it? This sounds like a front end styling problem not a PHP WordPress one – Tom J Nowell Apr 6 '15 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.