我正在开发一个 wordpress 主题,目前正在为该主题实现自定义标题功能。最初,我对将自定义标题图像应用到主题没有任何问题,但突然之间header_image()
开始返回一个值,例如:http://site_url/wp-content/themes/themename/img/headerimage.png
似乎该site_url()
函数没有被执行并且嵌入在返回值中header_image()
。但是当site_url()
单独使用时,它会返回正确的值。
// header.php 中用于显示图像的代码
<?php $header_image = get_header_image();
if ( ! empty( $header_image ) ) { ?>
<a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
<img src="<?php header_image(); ?>" width="<?php echo get_custom_header()->width; ?>" height="<?php echo get_custom_header()->height; ?>" alt="" />
</a>
<?php }
?>
// custom-header.php 中用于设置自定义标题的代码
function sens_gamer_custom_header_setup() {
$args = array(
'default-image' => get_template_directory_uri() . '/img/sensationalgamerlogo1.png',
'width' => 221,
'height' => 90,
'header-text' => false,
'wp-head-callback' => 'sens_gamer_header_style',
'admin-head-callback' => 'sens_gamer_admin_header_style',
'admin-preview-callback' => 'sens_gamer_admin_header_image',
'uploads' => true,
);
$args = apply_filters( 'sens_gamer_custom_header_args', $args );
if ( function_exists( 'wp_get_theme' ) ) {
add_theme_support( 'custom-header', $args );
} else {
// Compat: Versions of WordPress prior to 3.4.
define( 'HEADER_IMAGE', $args['default-image'] );
define( 'HEADER_IMAGE_WIDTH', $args['width'] );
define( 'HEADER_IMAGE_HEIGHT', $args['height'] );
add_custom_image_header( $args['wp-head-callback'], $args['admin-head-callback'], $args['admin-preview-callback'] );
}
}
add_action( 'after_setup_theme', 'sens_gamer_custom_header_setup' );
if ( ! function_exists( 'get_custom_header' ) ) {
function get_custom_header() {
return (object) array(
'url' => get_header_image(),
'thumbnail_url' => get_header_image(),
'width' => HEADER_IMAGE_WIDTH,
'height' => HEADER_IMAGE_HEIGHT,
);
}
}
if ( ! function_exists( 'sens_gamer_header_style' ) ) :
function sens_gamer_header_style() {
// If no custom options for text are set, let's bail
// get_header_textcolor() options: HEADER_TEXTCOLOR is default, hide text (returns 'blank') or any hex value
if ( HEADER_TEXTCOLOR == get_header_textcolor() && '' == get_header_image() )
return;
// If we get this far, we have custom styles. Let's do this.
?>
<style type="text/css">
</style>
<?php
}
endif; // sens_gamer_header_style