0

I have a banner image that appears on Pages that I want. However, I also have a Page designated as a Blog where it doesn't appear. Is there any way to have this banner appear on a Page that is using a blog template? Tried messing around with adding is_home() or is_page( $id ) to code below but I'm grasping at straws, appreciate your thoughts

//* Add Header Images on Posts/Pages
add_image_size( 'header-img', 1600);
add_action('after_header', 'site_banner', 0);
function site_banner(){
    if( !is_singular( 'page' ) )
        return;

    if( has_post_thumbnail() && is_page() ) :
        $ftbanner = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'header-img' );
        $banner = $ftbanner[0];
        echo '<div id="site-banner" class="site-banner">' . "\n";
        echo '<img src="' . $banner . '">' . "\n";
        echo '</div>' . "\n";
    endif;
}
1
  • Appreciate the attention to detail @Howdy_McGee
    – Chris
    Feb 19, 2015 at 22:11

1 Answer 1

1

Since you've designated a specific page to be your Post Archive ( blog ) it no longer is a standard page; meaning is_page() will likely fail. Since your blog page is now more of an archive, it is automatically being returned because of your first conditional.

  • is_singular() - Returns true when viewing a single page, post, or post type.

So what you're saying is IF we're NOT viewing a Singular Page then return. Since blog is no longer a singular page, instead, let's say:

if( ! is_page() && ! is_home() )
    return;

Next, since this blog page is an archive now we cannot reply on the global $post to give us the correct ID for the blog page, thus get_the_ID() will fail. What we need to do is get the blog ID using the following:

$blog_id = get_option( 'page_for_posts' );

I've taken some liberties below with your function but it should work the same.

//* Add Header Images on Posts/Pages
add_image_size( 'header-img', 1600 );
add_action( 'after_header', 'site_banner', 0 );
function site_banner() {
    if( ( is_page() || is_home() ) ) {
        $post_id = ( is_home() ? get_option( 'page_for_posts' ) : get_the_ID() );

        if( has_post_thumbnail( $post_id ) ) {
            $ftbanner = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'header-img' );
            $banner = $ftbanner[0];
            echo '<div id="site-banner" class="site-banner">' . "\n";
            echo '<img src="' . $banner . '">' . "\n";
            echo '</div>' . "\n";
        }
    }
}

What I'm doing differently here is right off the bat I make sure we are either viewing a page or the blog page and if we are, set the $post_id then verify that that page has a featured image set. IF it does have one set, show the banner image.

4
  • I'm kind of blown away right now, thank you @howdy_mcgee you nailed it! Especially appreciate how skillfully you explained and dissected this. I love PHP but I'm also completely humbled by it at times like this. Really appreciate your feedback!
    – Chris
    Feb 19, 2015 at 22:49
  • @Chris Thanks! If you found this helpful, don't forget to upvote / mark as answered ( checkmark ) so that future viewers know your question was addressed, and welcome to the WordPress community!
    – Howdy_McGee
    Feb 19, 2015 at 22:52
  • Hey @Howdy_McGee I just realized that my pages have the banner, but my posts themselves don't, is this correct: if( ( is_page() || is_home() || is_single() ) ) {
    – Chris
    Feb 22, 2015 at 15:12
  • @Chris that looks right, if you have other post types you could also check is_singular( 'post' ); which would only return true if viewing a single post of post-type post ( which is a tad confusing I know ).
    – Howdy_McGee
    Feb 22, 2015 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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