我目前正在使用Wordpress主题并尝试添加自定义设置控件。我有一个functions.php,我在这里添加一个设置和控件:
// =============================
// = 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的Theme Customizer中选择该选项。 现在我想查看我的主文档,选择哪个选项 - 但我没有得到任何价值。这是回应选择数组的代码。
<?php
echo get_theme_mod('radio_input');
?>
即使我更改了设置类型(复选框,文本输入,下拉列表),我也从未获得任何回报。如果我回显一个字符串(用于测试目的),我会看到字符串,但我无法从设置控件中获取值。我在这里做错了什么?
提前谢谢!
答案 0 :(得分: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,其中control 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',
),
));