0

I want to display, based on the current page another header image.

Short example:

  • Home -> Image A
  • PageId 1,2,3,4 -> Image B (2,3,4 are childs of page 1)
  • PageId 5, 6 -> Image C
  • ...

I tried to edit the header.php of my theme, because there is the coding block which displays the header image:

<?php
                    // The header image
                    // Check if this is a post or page, if it has a thumbnail, and if it's a big one
                    if ( is_singular() && has_post_thumbnail( $post->ID ) &&
                            ( /* $src, $width, $height */ $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), array( $header_image_width, $header_image_width ) ) ) &&
                            $image[1] >= $header_image_width ) :
                        // Houston, we have a new header image!
                        echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' );
                    else :
                        // Compatibility with versions of WordPress prior to 3.4.
                        if ( function_exists( 'get_custom_header' ) ) {
                            $header_image_width  = get_custom_header()->width;
                            $header_image_height = get_custom_header()->height;
                        } else {
                            $header_image_width  = HEADER_IMAGE_WIDTH;
                            $header_image_height = HEADER_IMAGE_HEIGHT;
                        }
                        // echo "here";
                        ?>

                    <img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
                <?php endif; // end check for featured image or standard header ?>

So I try to hook in with following elseif: (I removed the <?php ?> for easier reading)

if(is_home()){
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
}else if(is_page(1){
<img src="http://.../ImageB.png" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
}else{
<img src="<?php header_image(); ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
}

But that didn't work, instead no header image was shown.

How can I achieve this?

BR, mybecks

1 Answer 1

2

On the else if there is missing a closing bracket: } elseif ( is_page(1) ) {.

Since all you alter is the file name, it's sufficient to use the following code:

if ( is_home() ) $my_header_image = get_header_image();
elseif ( is_page(1) ) $my_header_image = "http://.../ImageB.png";
elseif ( is_... ) $my_header_image = ...;
else...
?>
<img src="<?php echo $my_header_image; ?>" width="<?php echo $header_image_width; ?>" height="<?php echo $header_image_height; ?>" alt="" />
4
  • So, have you been able to achieve want you asked for?
    – tfrommen
    Mar 7, 2013 at 11:57
  • I tried your solution, but it didin't work. I'm using the theme twenty eleven as base.
    – mybecks
    Mar 8, 2013 at 7:36
  • Unfortunately, I first forgot an echo inside the image tag (which I edited/corrected 20 hours ago). Did you see that and included it into your code, or have you tried it earlier (and thus without the missing echo)?
    – tfrommen
    Mar 8, 2013 at 7:59
  • Ohh, but I overlooked that too. But now it works. ;-)
    – mybecks
    Mar 12, 2013 at 9:32

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.