我使用的是一个自定义主题,它是从样板(不久前)开发的。我注意到,当设置为静态时,标题图像可以正常工作,但当设置为随机时,标题图像无法显示。我尝试切换主题,并意识到同样的问题也会发生在样板和2112上。然而,二十点十分有效,所以我将其代码复制到了我的主题中,但仍然不起作用!
我的职能。php包含:
// 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;
基本上是逐字复制。
还有我的头球。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
?>
我现在都快疯了,也许我还缺什么?还是我没有注意到的冲突?
SO网友:montrealist
检查了代码,看起来里面出现了随机标题图像功能get_header_image()
. 因此,问题可能是以下之一:
您的elseif 永远不会执行。换句话说if 条件的计算结果始终为true,并且get_the_post_thumbnail()
负责生成图像您的主题不支持启用custom-header, default-image, 或者两者兼而有之为了快速准确地诊断发生了什么,我将在您的header.php 看看发生了什么。
这是source code 属于get_header_image()
供参考。
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: 刚刚发现了一些东西。尝试添加
default-image
参数添加到数组中以进行add\\u theme\\u支持调用,如下所示:
$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 在理解哪个参数负责启用主题中的随机图像时非常有用。