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="https://wordpress.stackexchange.com/questions/93900/<?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?
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:
- 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. - 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.