0

I'm just looking to make sure my home page and pages display my featured image if set, and other wise if not set show blog-banner.jpg. So that's what I thought I had however now my 404 error page doesn't have a banner image. Any advice on correcting this if statement?

/** Add the featured image section */
add_action( 'genesis_before_header', 'minimum_featured_image' );
function minimum_featured_image() {

global $post; 
$post->ID;

if ( is_home() ) {
    echo '<div id="featured-image-home">';
    echo get_the_post_thumbnail($thumbnail->ID, 'header');
    echo '</div>';
}
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>';
}
}
0

The second if statement requires it to be a post, so a 404 page won't pass.

I'd re-do it like this, using elseif statements. I also changed $thumbnail->ID to $post->ID since the former was not defined.

add_action( 'genesis_before_header', 'minimum_featured_image' );
function minimum_featured_image() {

    global $post; 

    if ( is_home() ) {
        echo '<div id="featured-image-home">';
        echo get_the_post_thumbnail($post->ID, 'header');
        echo '</div>';
    }
    elseif ( is_singular( array( 'post' , 'page' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
        echo '<div id="featured-image">';
        echo get_the_post_thumbnail($post->ID, 'header');
        echo '</div>';
    }   
    elseif(is_singular( array( 'post' , 'page' ) ){
        echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
    }
} 
  • Thanks for the speedy reply jetlej! It is saying I have an error on this line "elseif(is_singular( array( 'post' , 'page' ) ){". Do you know why that might be? – kia4567 Apr 8 '15 at 5:51
  • What exactly is the error? – jetlej Apr 8 '15 at 17:39
  • It just says there is an error on the function.pp file on line "such and such". When I go to that line, the second elseif statement line is what is throwing an error. – kia4567 Apr 9 '15 at 0:16
  • I basically want the pages to show the featured image but if no featured image show blog-banner.jpg. Maybe this statement can be even smaller to encompass all pages of wordpress. – kia4567 Apr 9 '15 at 0:18
0

You need one more conditional

if ( is_404() ) {
    // your code here
}

Your function:

add_action( 'genesis_before_header', 'minimum_featured_image' );
function minimum_featured_image() {

global $post; 

if ( is_home() ) {
    echo '<div id="featured-image-home">';
    echo get_the_post_thumbnail($post->ID, 'header');
    echo '</div>';
}
elseif ( is_singular( array( 'post' , 'page' ) ) && ( !has_post_thumbnail( $post->ID ) ) ){
    echo '<div id="featured-image">';
    echo get_the_post_thumbnail($post->ID, 'header');
    echo '</div>';
}   
elseif( is_singular( array( 'post' , 'page' ) ) ) {
    echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
}
elseif( is_404() ) {
    echo '<div id="featured-image-home"><img src="'. get_stylesheet_directory_uri() . '/images/blog-banner.jpg" /></div>';
}

}

You might also want to check the rest of the conditional tags

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.