2015-01-15 22:11
浏览 28

如何输出单选按钮变量前端 - wordpress

I am working on my themes options panel (in administration/backend) and I am struggling with radio buttons.

I followed this tutorial: for creating radio buttons. They are now in themes options but I don't know how to make output of it into themes frontend.

I want just to try to echo the value of radio buttons form but I don't know the name of the variable in which it is saved.

Normally in php I would do it like so: if ($_POST['NAME']=="VALUE1") { echo "Some text here"; }

With text fields I use just it: <?php echo $options['csscolor_setting']; ?> (in for example header.php)
And in functions.php i have:

function csscolor_setting() {

    $options = get_option('theme_options');  echo "<input name='theme_options[csscolor_setting]' type='text' value='{$options['csscolor_setting']}' />";


But it's not possible with radio buttons. For now it would be enough if I know how to make some code like this real:

<?php if ($some_variable == 'yes')
{echo 'Something';}

Or just <?php echo $some_variable; ?>
But this $some_variable I can't find in my code.

Here is my code in functions.php regarding to radio buttons.

add_settings_field( 'sample_radio_buttons', __( 'Allow triangles in background?', 'YourTheme' ), 'YourTheme_settings_field_sample_radio_buttons', 'theme_options', 'general' );

Creating options for radio buttons field

function YourTheme_sample_radio_button_choices() {

    $sample_radio_buttons = array(

        'yes' => array(

            'value' => 'yes',

            'label' => 'Yes'


        'no' => array(

            'value' => 'no',

            'label' => 'No'



    return apply_filters( 'YourTheme_sample_radio_button_choices', $sample_radio_buttons );


Creating sample radio buttons field

function YourTheme_settings_field_sample_radio_buttons() {

    $options = YourTheme_get_theme_options();

    foreach ( YourTheme_sample_radio_button_choices() as $button ) {


    <div class="layout">

        <label class="description">

            <input type="radio" name="YourTheme_theme_options[sample_radio_buttons]" value="<?php echo esc_attr( $button['value'] ); ?>" <?php checked( $options['sample_radio_buttons'], $button['value'] ); ?> />

            <?php echo $button['label']; ?>






Getting the current options from the database and setting deafaults.

function YourTheme_get_theme_options() {

        $saved = (array) get_option( 'YourTheme_theme_options' );

        $defaults = array(

            'sample_checkbox'       => 'off',

            'sample_text_input'     => '',

            'sample_select_options' => '',

            'sample_radio_buttons'  => 'yes',

            'sample_textarea'       => '',


        $defaults = apply_filters( 'YourTheme_default_theme_options', $defaults );

        $options = wp_parse_args( $saved, $defaults );

        $options = array_intersect_key( $options, $defaults );

        return $options;


Then there is a little bit more code about sanitization and validation but I think it should not have any inffluence on variable in form.

Thank you in advance.

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题