2017-05-03 6 views
0

私は現在Wordpress Themeでウォーキングしており、カスタマイズ設定コントロールを追加しようとしています。私はこのような設定とコントロールを追加する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のTheme Customizerでオプションを選択できます。 今私の主な文書をチェックインしたいのですが、どの選択肢が選択されていますか?値は戻っていません。次に、choices配列をエコーするコードを示します。

<?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', 
    ), 
));