我正在开发一个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
答案 0 :(得分:0)
我有同样的问题。我没有解决方案,但这种解决方法对我有用:
<img src="<?php echo( str_replace('http://site_url', site_url(), get_header_image()) ); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />