默认图像显示为空白 - Wordpress主题自定义API

时间:2014-02-19 07:09:26

标签: wordpress wordpress-theming

我希望我的主题用户可以通过Theme Customization API替换横幅。我有它工作,但是,默认图像显示为空白,当我点击“查看页面源”时,我得到以下内容:

<img src=" " alt="banner" />

默认图像显示在API窗口内的预览中,但不显示在浏览器中。我上传横幅以替换默认横幅的那一刻,它完美无缺。但我只是无法显示默认图像。我做错了吗?

这是我的functions.php:

// Start New Section for Images
$wp_customize->add_section('customtheme_images', array(
'title' => _('Images'),
'description' => 'Change Images'
));
$wp_customize->add_setting('banner_image', array(
'default' => 'http://mywebsite.com/banner.jpg',
));
$wp_customize->add_control( new WP_Customize_Image_Control ($wp_customize,   'banner_image', array(
'label' => _('Change Banner'),
'section' => 'customtheme_images',
'settings' => 'banner_image'
) )); 

而且,这是我的header.php:

<img src="<?php echo get_theme_mod('banner_image'); ?>" alt="banner">

是的,我已经检查了默认图像的路径,它是正确的。请帮忙!

2 个答案:

答案 0 :(得分:3)

使用$wp_customize->add_setting('banner_image', array( 'default' => 'http://mywebsite.com/banner.jpg', ));时,默认值不会保存到数据库中(直到您保存)。

所以你必须使用:<img src="<?php echo get_theme_mod('banner_image','http://mywebsite.com/banner.jpg'); ?>" alt="banner">

您描述的问题与https://wordpress.stackexchange.com/questions/129479/alternatives-to-handle-customizer-settings

有关

答案 1 :(得分:0)

我这里也有同样的情况。以下解决方案的问题:

<?php echo get_theme_mod('banner_image','http://mywebsite.com/banner.jpg'); ?>

我需要用户能够删除默认图像。此默认图像将仅用于主题的初始显示。所以我需要一个在激活主题时已经保存在数据库中的解决方案。