【问题标题】:Proper way to get value from Wordpress customize control从 Wordpress 自定义控件中获取价值的正确方法
【发布时间】:2017-05-03 15:41:39
【问题描述】:

我目前正在使用 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 的主题定制器中选择选项。 现在我想检查我的主文档,选择了哪个选项 - 但我没有得到任何值。这是回显选择数组的代码。

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

即使我更改了设置类型(复选框、文本输入、下拉菜单),我也永远无法返回任何值。如果我回显一个字符串(出于测试目的),我会看到该字符串,但我无法从设置控件中获取值。我在这里做错了什么?

提前谢谢你!

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    我认为,section id 和 control 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',
        ),
    ));
    

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 2020-01-22
      • 2016-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      • 2013-10-01
      相关资源
      最近更新 更多