Wordpress Genesis theme display a custom header/background image using featured image

678 views Asked by At

I am building a site using the Genesis framework. I ran into a problem with adding a custom header to each page and post using the featured image link in the admin area. Right now, the featured image header won't show on the page I assign to be the "Blog" page under the "Reading" settings.

Here is the site URL, http://ajcustomfinishes.starfireclients.com/.

Here's the function I am using:

// Create new image size for our hero image
add_image_size( 'hero-image', 1400, 400, TRUE ); // creates a hero image size

// Hook after header area
add_action( 'genesis_after_header', 'bw_hero_image' );

function bw_hero_image() {
// If it is a page and has a featured thumbnail, but is not the front page do the following...
    if (has_post_thumbnail() && is_page() ) {
        // Get hero image and save in variable called $background
        $image_desktop = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'hero-image' );
        $image_tablet = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'large' );
        $image_mobile = wp_get_attachment_image_src( get_post_thumbnail_id( $page->ID ), 'medium' );

        $bgdesktop = $image_desktop[0];
        $bgtablet = $image_tablet[0];
        $bgmobile = $image_mobile[0];

// You can change above-post-hero to any class you want and adjust CSS styles
        $featured_class = 'above-post-hero';

 ?> 
<div class='<?php echo $featured_class; ?>'><h1 class="custom-title">AJ Customs Finishes in Las Vegas<br>Call 702-795-7338 today!</h1></div>
<style>
    <?php echo ".$featured_class "; ?> {background-image:url( <?php echo $bgmobile; ?>);height:176px;}

        @media only screen and (min-width : 480px) {       
        <?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgtablet;?>);height:276px;}
        }
        @media only screen and (min-width : 992px) {       
        <?php echo ".$featured_class "; ?> {background-image:url(<?php echo $bgdesktop;?>);height:400px;}
        }
</style>
<?php
    } 
}

Does anybody know how I can use the featured image to display a custom header on each page?

1

There are 1 answers

1
Muhammad Asadullah On BEST ANSWER

You have this condition in your code, which will be FALSE in case of page you set as blog in reading settings:

    if (has_post_thumbnail() && is_page() ) {

is_page() will be false on blog posts home (page you set for posts in reading).

You need to set this condition as is_home() to be true i.e. you can repurpose your code to be:

    if (has_post_thumbnail() && is_page() || is_home()) {