Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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="" />
share|improve this answer
    
So, have you been able to achieve want you asked for? –  t f Mar 7 '13 at 11:57
    
I tried your solution, but it didin't work. I'm using the theme twenty eleven as base. –  mybecks Mar 8 '13 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)? –  t f Mar 8 '13 at 7:59
    
Ohh, but I overlooked that too. But now it works. ;-) –  mybecks Mar 12 '13 at 9:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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