Php 单选按钮选择值保留
我正在使用WordPress设置页面,该页面有一个单选按钮,带有两个给定选项“是”或“否”,但它似乎没有保留所选选项,刷新后会取消选择。选中的选项保持选中状态非常重要,因为它会触发元素的显示。我已经研究并试用了下面的PHP函数,但它仍然没有保留所选的选项Php 单选按钮选择值保留,php,wordpress,Php,Wordpress,我正在使用WordPress设置页面,该页面有一个单选按钮,带有两个给定选项“是”或“否”,但它似乎没有保留所选选项,刷新后会取消选择。选中的选项保持选中状态非常重要,因为它会触发元素的显示。我已经研究并试用了下面的PHP函数,但它仍然没有保留所选的选项 function section_footer() {} add_settings_field('socialbar', 'Display Social Bar', 'socialbar', __FILE__, 'footer_sett
function section_footer() {}
add_settings_field('socialbar', 'Display Social Bar', 'socialbar', __FILE__, 'footer_settings' );
}
function socialbar() {
$options = get_option('theme_options'); echo "<input name='theme_options[socialbar]' type='radio' value='yes' />"; echo 'Yes'; echo " ";
if (isset($socialbar) && $socialbar=="yes") echo "checked";
$options = get_option('theme_options'); echo "<input name='theme_options[socialbar]' type='radio' value='no' />"; echo 'No';
if (isset($socialbar) && $socialbar=="no") echo "checked";
}
函数节_footer(){}
添加“设置”字段(“社交栏”、“显示社交栏”、“社交栏”、“社交栏”、“文件”、“页脚设置”);
}
函数socialbar(){
$options=get_option('theme_options');echo“”;echo'Yes';echo“”;
如果(isset($socialbar)&&$socialbar==“是”)回显“选中”;
$options=get_option('theme_options');echo“”;echo'No';
如果(isset($socialbar)&&$socialbar==“否”)回显“已选中”;
}
您需要回显输入声明内部的“checked”
将您的PHP更改为以下内容。可以使用三元运算符内联执行if语句
function socialbar() {
$options = get_option('theme_options'); echo "<input name='theme_options[socialbar]' type='radio' value='yes' ".(isset($socialbar) && $socialbar=="yes" ? "checked" : "")." />";
echo 'Yes'; echo " ";
$options = get_option('theme_options'); echo "<input name='theme_options[socialbar]' type='radio' value='no' ".(isset($socialbar) && $socialbar=="no" ? "checked" : "")." />"; echo 'No';
函数socialbar(){
$options=get_option('theme_options');echo“”;
回音“是”;回音“是”;
$options=get_option('theme_options');echo“”;echo'No';
}在输出
已检查的之前,您正在关闭
标记–因此已检查的不是该输入元素的属性,而是它后面的普通文本。(您的函数甚至不知道变量$socialbar
,因为它不在其范围内,也没有作为参数传入。)感谢您的更正。我如何安全地将它们结合起来?Mituw16它仍然没有保留所选的值。你也可以发布你的HTML吗?我只是将您现有的PHP转换为WordPress函数中的三元if.All PHP。