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'm using a custom theme, developed from Boilerplate (some time ago). I noticed that the header images work fine when set statically, but fail to appear when set to random. I tried switching themes, and realized the same issue happens with Boilerplate, as well as Twentytwelve. Twentyten worked though, so I copied its code to my theme, and STILL it doesn't work!

My functions.php contains:

    // The custom header business starts here.

$custom_header_support = array(
    // The default image to use.
    // The %s is a placeholder for the theme template directory URI.
    'default-image' => '%s/images/headers/starkers.png',
    // The height and width of our custom header.
    'width' => 760,
    'height' => 280,
    // Don't support text inside the header image.
    'header-text' => false,
    // Callback for styling the header preview in the admin.
    'admin-head-callback' => '',
);

add_theme_support( 'custom-header', $custom_header_support );

if ( ! function_exists( 'get_custom_header' ) ) {
    // This is all for compatibility with versions of WordPress prior to 3.4.
    define( 'HEADER_TEXTCOLOR', '' );
    define( 'NO_HEADER_TEXT', true );
    define( 'HEADER_IMAGE', $custom_header_support['default-image'] );
    define( 'HEADER_IMAGE_WIDTH', $custom_header_support['width'] );
    define( 'HEADER_IMAGE_HEIGHT', $custom_header_support['height'] );
    add_custom_image_header( '', $custom_header_support['admin-head-callback'] );
    add_custom_background();
}

// We'll be using post thumbnails for custom header images on posts and pages.
// We want them to be 940 pixels wide by 198 pixels tall.
// Larger images will be auto-cropped to fit, smaller ones will be ignored. See header.php.
set_post_thumbnail_size( $custom_header_support['width'], $custom_header_support['height'], true );

// ... and thus ends the custom header business.


// Default custom headers packaged with the theme. %s is a placeholder for the theme template directory URI.
register_default_headers( array(
    'berries' => array(
        'url' => '%s/images/headers/starkers.png',
        'thumbnail_url' => '%s/images/headers/starkers-thumbnail.png',
        /* translators: header image description */
        'description' => __( 'Boilerplate', 'boilerplate' )
    )
) );
}
endif;

Basically copied verbatim.

And my header.php:

<?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' );
    elseif (get_header_image()) : ?>
    <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 
?>

I'm JUST about out of my mind with it now—is there something I'm missing, maybe? Or a conflict I haven't noticed?

share|improve this question
add comment

2 Answers

Checked the code and it looks like the random header image functionality is happening inside get_header_image(). So the issue could be one of the following:

  1. Your elseif never gets executed. In other words, the if condition always evaluates to true and get_the_post_thumbnail() takes care of generating the image.
  2. Your theme does not support enabled for either custom-header, default-image, or both.

For a quick way to diagnose exactly what's happening I would put debugging messages everywhere in your header.php to see what's going on.

Here's the source code of get_header_image() for reference.

function get_header_image() {
    $url = get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );

    if ( 'remove-header' == $url )
        return false;

    if ( is_random_header_image() )
        $url = get_random_header_image();

    return esc_url_raw( set_url_scheme( $url ) );
}

Edit: just found something. Try adding default-image param to your array for add_theme_support call, like so:

$custom_header_support = array(
    // The default image to use.
    // The %s is a placeholder for the theme template directory URI.
    'default-image' => '%s/images/headers/starkers.png',
    // The height and width of our custom header.
    'width' => 760,
    'height' => 280,
    // Don't support text inside the header image.
    'header-text' => false,
    // Callback for styling the header preview in the admin.
    'admin-head-callback' => '',
    'random-default' => true
);

Note: This link was super-useful in understanding which parameter is responsible for enabling random images in themes.

share|improve this answer
    
Thanks @dalbaeb! get_header_image() returns empty, so you may be right and somehow my theme is blocking support? If I set the header to just ONE static image, it returns the image, but when it's set to random, it doesn't. However, I was under the impression that add_theme_support( 'custom-header', $custom_header_support ); would enable support for the custom header, and since it's working for static images, I assume that the support is, in fact, enabled. Would there be a reason that the random image wouldn't return? –  Sarah London Semark Mar 29 '13 at 21:20
    
Check output for each of the following in your functions.php: is_random_header_image(), and get_theme_mod( 'header_image', get_theme_support( 'custom-header', 'default-image' ) );, it will give you more clues. –  dalbaeb Mar 29 '13 at 21:40
    
is_random_header_image() returns true. get_theme_mod returns just "random-uploaded-image" ... is that the default image? I'm not sure I entirely understand what's going on. –  Sarah London Semark Mar 29 '13 at 22:30
    
I'm not sure, hard to tell without having a copy of your code. You can also check output of is_random_header_image(), and get_random_header_image(). Mine are 1 (true) and a URL string, respectively. So if yours are different then something isn't right somewhere. –  dalbaeb Mar 29 '13 at 22:42
    
@SarahLondonSemark just added something to my answer, try it out. –  dalbaeb Mar 29 '13 at 22:48
show 1 more comment

Should you be using the Post Types Order plugin:
We had the same issue, and solved it by disabling the Auto-Sort feature of that plugin.

share|improve this answer
add comment

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.