2017-05-03 30 views
0

我目前正在woking一個WordPress的主題,並試圖添加自定義設置控件。我有一個functions.php我添加一個設置和控制像這樣:正確的方式來從WordPress的價值定製控制

// ============================= 
// = Radio Input    = 
// ============================= 
$wp_customize->add_setting('radio_input', array(
    'default'  => 'value2', 
    'capability'  => 'edit_theme_options', 
    'type'   => 'theme_mod', 
)); 

$wp_customize->add_control('themename_color_scheme', array(
    'label'  => __('Radio Input', 'themename'), 
    'section' => 'themename_color_scheme', 
    'settings' => 'radio_input', 
    'type'  => 'radio', 
    'choices' => array(
     'value1' => 'Choice 1', 
     'value2' => 'Choice 2', 
     'value3' => 'Choice 3', 
    ), 
)); 

它的工作原理。我可以在Wordpress中的主題定製器中選擇該選項。 現在我想檢查我的主文檔,選擇了哪個選項 - 但我沒有收到任何值。這裏是回顯選擇數組的代碼。

<?php 
    echo get_theme_mod('radio_input'); 
?> 

即使我更改設置類型(複選框,文本輸入,下拉)我從來沒有得到任何價值了。如果我回顯一個字符串(用於測試目的),我會看到字符串,但我無法從設置控件中獲取值。我在這裏做錯了什麼?

預先感謝您!

回答

0

我認爲,該部分ID和控制ID應該是不同的。在你的代碼是相同的:

// CONTROL ID HERE IS THE SAME, AS SECTION ID 'themename_color_scheme' 
$wp_customize->add_control('themename_color_scheme', array(
    'label'  => __('Radio Input', 'themename'), 
    'section' => 'themename_color_scheme', // SECTION ID 
    'settings' => 'radio_input', 
    'type'  => 'radio', 
    'choices' => array(
     'value1' => 'Choice 1', 
     'value2' => 'Choice 2', 
     'value3' => 'Choice 3', 
    ), 
)); 

工作對我來說:(這應該有助於爲 '經典' 控制類型,例如文本)

// ============================= 
// = Text Input    = 
// ============================= 
$wp_customize->add_setting('__UNIQUE_SETTING_ID__', array( // <-- Setting id 
    'capability'  => 'edit_theme_options', 
    'type'   => 'theme_mod', 
)); 

$wp_customize->add_control('__UNIQUE_CONTROL_ID__', array( // <-- Control id 
    'label'   => __('Text Input', 'themename'), 
    'section'  => '__UNIQUE_SECTION_ID__',   // <-- Section id 
    'settings'  => '__UNIQUE_SETTING_ID__'    // Refering to the settings id 
)); 

!但是!如果選擇類型(無線電類型可能是相同的情況下),我仍然沒有得到價值。這裏幫助我this article,其中控制ID與設置ID相同:

// ============================= 
// = Select Input    = 
// ============================= 
$wp_customize->add_setting('__UNIQUE_SETTING_ID__', array( // <-- Setting id 
    'default'  => 'value2', 
    'capability'  => 'edit_theme_options', 
    'type'   => 'theme_mod', 
)); 

$wp_customize->add_control('__UNIQUE_SETTING_ID__', array( // <-- THE SAME Setting id 
    'label'   => __('Select Input', 'themename'), 
    'section'  => '__UNIQUE_SECTION_ID__',   // <-- Section id 
    // 'settings'  => '__UNIQUE_SETTING_ID__',   // Ignoring settings id here 
    'type'   => 'select', 
    'choices'  => array(
     'value1'  => 'Choice 1', 
     'value2'  => 'Choice 2', 
     'value3'  => 'Choice 3', 
    ), 
)); 
相關問題