使用wp_customize在wordpress菜单中添加自定义部分以选择图像

时间:2017-05-06 20:12:06

标签: php html wordpress

我的function.php文件中有以下功能;

function customize_images($wp_customize){

$wp_customize->add_section( 'customize_images_save', array(
    'title'    => __('Custom Image', 'themename'),
    'description' => 'Select Image',
    'priority' => 120,
));

    $wp_customize->add_setting( 'image_option', array(
        'default'           => 'image.jpg',
        'capability'        => 'edit_theme_options',
        'type'           => 'option',

    ));

    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'image_option', array(
        'label'    => __('Select Image', 'themename'),
        'description' => '',
        'section'  => 'customize_images_save',
        'priority'    => 10,
        'settings' => 'image_option',
    )));

}

应该在我的WordPress自定义菜单中添加自定义部分。

然后使用以下代码显示image in my home-page.php文件;

<?php 
    $my_image_top = get_theme_mod( 'image_option', 'default.jpg' );
    $image_top = wp_get_attachment_image_src( $my_image_top , 'full' ); 
?>

<img src="<?php echo $image_top ?>">

不幸的是,它没有显示任何内容,只是返回空白<img src"">。我一直在努力找到错误,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

发现问题,这里是$wp_customize->add_setting中的更正。

这是来自WordPress Codex的设置参考;

<?php add_settings_field( $id, $title, $callback, $page, $section, $args ); ?>

错误的陈述旧代码;

$wp_customize->add_setting( 'image_option', array(
    'default'           => 'image.jpg',
    'capability'        => 'edit_theme_options',
    'type'           => 'option',

));

我选择了图像选择器'type' => 'option'

以下是修改此问题的已编辑代码:

$wp_customize->add_setting( 'image_option', array(
        'default'           => 'image.jpg',
));

我希望将来可以帮到某个人。有关此处$wp_customize->add_setting用法的更多信息:WordPress Function Reference/add settings field