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

How to output radio buttons variable to frontend - wordpress



我正在处理我的主题选项面板(在 administration/backend 中),我正在努力使用 radio buttons.

我遵循了本教程:https://github.com/cferdinandi/wp-theme-options/ 创建单选按钮。它们现在位于主题选项中,但我不知道如何将其输出到主题前端。

我只想尝试 echo 单选按钮的值形式,但我不知道保存它的变量的名称。

通常在 php 我会这样做: if ($_POST['NAME']=="VALUE1") { echo "Some text here"; }

对于文本字段,我只使用它:<?php echo $options['csscolor_setting']; ?>(例如 header.php)
在 functions.php 我有:

function csscolor_setting() {

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

}

但是使用单选按钮是不可能的。现在,如果我知道如何制作这样的真实代码就足够了:

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

或者只是 <?php echo $some_variable; ?>
但是这个 $some_variable 我在我的代码中找不到。

这是我在 functions.php 中关于单选按钮的代码。

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


为单选按钮字段创建选项

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 );

}


创建示例单选按钮字段

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

    }

}


正在从数据库获取当前选项并设置默认值。

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;

    }


然后还有一些关于清理和验证的代码,但我认为它不应该对表单中的变量有任何影响。

提前致谢。

在前端,您可以使用与您在 get_theme_options 函数中使用的函数相同的函数,即:

get_option( 'YourTheme_theme_options' );

看看这个:http://codex.wordpress.org/Function_Reference/get_option

感谢您的回答。实际上,我还需要向 echo 选中的单选按钮的值添加更多代码。

我在前端的代码(例如 footer.php)如下所示:

<?php $YourTheme_theme_options = get_option('YourTheme_theme_options');
echo $YourTheme_theme_options['sample_radio_buttons']; ?>

我希望这对开发主题选项页面的人有所帮助。