2017-05-06 63 views
0

我在我的function.php文件中具有以下功能;使用wp_customize在wordpress菜單中添加自定義部分以選擇圖像

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"">。我一直在努力尋找錯誤,有什麼幫助?

回答

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

相關問題