2014-04-03 191 views
0

我正在设置一个WordPress设置页面,该页面上有一个单选按钮,其中有两个给定选项'yes'或'no',但它似乎不保留所选选项,它在刷新后取消选择。选定的选项保持选定状态非常重要,因为它会触发元素的显示。我已经研究并尝试了下面的PHP函数,但它仍然不保留所选的选项。单选按钮选择保留值

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 "&nbsp;&nbsp;&nbsp;"; 
      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"; 

    } 
+0

你输出'checked'之前_closing_你''标签 - 所以'checked'不是输入部件后它的属性,但只是正常_text_。 (并且你的函数甚至不知道一个变量'$ socialbar',因为它不在它的范围内,也没有作为参数传入。) – CBroe

+0

感谢您的更正。我如何安全地将它们结合起来? –

回答

1

您需要在输入声明中回显“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 "&nbsp;&nbsp;&nbsp;"; 

    $options = get_option('theme_options'); echo "<input name='theme_options[socialbar]' type='radio' value='no' ".(isset($socialbar) && $socialbar=="no" ? "checked" : "")." />"; echo 'No'; 

}

+0

Mituw16它仍然没有保留选定的值 –

+0

你可以发布你的HTML吗?我只是把你现有的PHP转换成一个三元组。 – mituw16

+0

所有PHP中的WordPress功能。 –