doujiao7325 2015-01-15 22:11
浏览 29
已采纳

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



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

I followed this tutorial: https://github.com/cferdinandi/wp-theme-options/ 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']; ?>

        </label>

    </div>

    <?php

    }

}


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条回答 默认 最新

  • duandangqin0559 2015-01-15 22:19
    关注

    On the frontend you can just use the same function as the one you're using in your get_theme_options function, namely:

    get_option( 'YourTheme_theme_options' );
    

    Have a look at this: http://codex.wordpress.org/Function_Reference/get_option

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?