主题选项页面中的类别列表

时间:2012-08-24 作者:Wendy99966

我试图在主题选项页面中创建多选类别列表,所有内容都会显示出来,但当我保存选项时,只有多选框中最后一个选中的选项会保存到数据库中。

有人知道我做错了什么吗?

以下是我当前使用的代码片段:

<?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

任何帮助都将不胜感激。

2 个回复
最合适的回答,由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="...

SO网友:Wendy99966

感谢Devin的帮助,这就是我最终的答案,以防其他人偶然发现。

<?php
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="themename_theme_options[themename_content_bullet_cats][]" id="<?php echo $themename_option_name.\'[\'.$valueid.\']\'; ?>" multiple="multiple" size="8">
            <?php /* Get the list of categories */ 
            $cats_selected = $options[$valueid];
            $categories = get_categories();
            foreach ( $categories as $category) :
            ?>
            <option value="<?php echo $category->cat_ID; ?>" <?php if ( $cats_selected && in_array( $category->cat_ID, $cats_selected ) ) { echo \'selected="selected"\'; }?>><?php echo $category->cat_name; ?></option>
            <?php endforeach; ?>
        </select>
    </div>
<?php
break; 

结束

相关推荐

自定义POST类型固定链接发送到404.php

您好,我已经创建了自定义帖子类型和自定义分类法。自定义帖子类型页面名为page portfolio。php。我在其中创建了一个循环,我试图链接到每个帖子的单个页面。为此,我创建了一个单一投资组合模板。php,但由于某种原因,当我单击永久链接时,我会被发送到404。php页面。这是我的整个结构代码://custom post type and taxonomy declaration <?php add_action(\'init\' , \'portfolio_manage