我正在处理我的主题选项面板(在管理/后端),并且我正在努力使用单选按钮。
我遵循了本教程:创建单选按钮的 https://github.com/cferdinandi/wp-theme-options/。它们现在在主题选项中,但我不知道如何将它的输出转换为主题前端。
我只想尝试echo
单选按钮形式的值,但我不知道保存它的变量的名称。
通常在 php 中我会这样做:if ($_POST['NAME']=="VALUE1") { echo "Some text here"; }
对于文本字段,我只使用它:<?php echo $options['csscolor_setting']; ?>
(例如标题.php)
在函数中.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_变量我在我的代码中找不到。
这是我在函数中的代码.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
选中单选按钮的值。
我在前端的代码(例如页脚.php)如下所示:
<?php $YourTheme_theme_options = get_option('YourTheme_theme_options');
echo $YourTheme_theme_options['sample_radio_buttons']; ?>
我希望这将有助于某人开发主题选项页面。