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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器