个性化阅读
专注于IT技术分析

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

点击下载

我正在主题设置面板(在管理/后端)中工作, 并且我在使用单选按钮。

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

我只想尝试回显单选按钮窗体的值, 但我不知道将其保存在其中的变量的名称。

通常在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;

    }

然后, 还有更多有关清理和验证的代码, 但我认为它不应对形式变量产生任何影响。

先感谢你。


#1


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

get_option( 'YourTheme_theme_options' );

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


#2


谢谢你的回答。实际上, 我还需要添加一些代码来回显选中的单选按钮的值。

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

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

我希望这将有助于开发主题选项页面。

赞(0)
未经允许不得转载:srcmini » 如何将单选按钮变量输出到前端

评论 抢沙发

评论前必须登录!