我试图在主题选项页面中创建多选类别列表,所有内容都会显示出来,但当我保存选项时,只有多选框中最后一个选中的选项会保存到数据库中。
有人知道我做错了什么吗?
以下是我当前使用的代码片段:
<?php
$themename_themename = "themename";
$themename_shortname = "themename";
$themename_option_group = $themename_shortname.\'_theme_option_group\';
$themename_option_name = $themename_shortname.\'_theme_options\';
// WordPress Categories via an Array
$wp_cats = array();
$categories = get_categories(\'hide_empty=0&orderby=name\');
foreach ($categories as $category_list) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
$categories_tmp = array_unshift($wp_cats, "Select a category:");
// Create theme options
global $themename_settings;
$themename_settings = array (
// Multi-select
array( "name" => __( \'Cat List\',\'themename\'), \'id\' => $themename_shortname.\'_list_cats\', \'type\' => \'multi-select\',
"desc" => __( \'Multi Select Unorderd Lists in content area.\',\'themename\'),
\'std\' => \'\',
"value" => $wp_cats ),
);
function themename_settings_page() {
case \'multi-select\': // Multi-select ?>
<div class="options_input options_select">
<div class="options_desc"><?php echo $value[\'desc\']; ?></div>
<span class="labels"><label for="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>"><?php echo $value[\'name\']; ?></label></span>
<select name="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" id="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" multiple="multiple" style="height:150px; min-width: 200px;">
<?php
$multi_setting = $options[$valueid];
foreach ($value[\'value\'] as $category_list) {
?>
<option value="<?php echo $category_list ?>" <?php if($multi_setting != \'\') { selected($options[$valueid] == $category_list );} else { selected($options[$valueid] == $value[\'std\']);} ?>><?php echo $category_list; ?></option>
<?php } ?>
</select>
</div>
<?php
break;
} // --themename_settings_page
这是一个临时工。正在进行的屏幕截图:
Screenshot 任何帮助都将不胜感激。
最合适的回答,由SO网友:Devin Humbert 整理而成
这可能是HTML问题。对于多选,的名称需要以“[]”结尾,以表示数组。因此,请尝试更改:
<select name="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" id="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" multiple="multiple" style="height:150px; min-width: 200px;">
至
<select name="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>[]" id="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" multiple="multiple" style="height:150px; min-width: 200px;">
因此,您最终应该:
<select name="hurst_theme_options[hurst_content_bullet_cats][]" id="...