将下拉列表转换为wordpress网站中的多选复选框

时间:2016-01-27 06:03:47

标签: php wordpress

我正在尝试在我的网站的wordpress主题文件中添加一些自定义编码。

目前以下代码在下拉框中显示值,我想将其转换为多选复选框

<!-- Property Furnishing -->
        <div class="control-group">
            <label for="property-furnishing" class="control-label">
                <?php _e( 'Property Furnishing', 'realexpert' ); ?>
            </label>
            <div class="controls">
                <select name="property-furnishing" class="submit-select">
                <?php
                    $args = array(
                        'type' => 'property',
                        'taxonomy' => 'property-furnishing',
                        'hide_empty' => 0,
                    );
                    $cats = get_categories($args);
                    foreach( $cats as $cat ){
                        if($cat->slug == $pro['furnishing']){
                            $selected = 'selected';
                        }else{
                            $selected = '';
                        }
                        echo '<option value="'.$cat->slug.'" '.$selected.'>'.$cat->name.'</option>';
                    }
                ?>
                </select>
            </div>
        </div>

我试过用这个。这样好吗?

        <form name="property-furnishing[]" class="submit-select">
        <?php
            $args = array(
                'type' => 'property',
                'taxonomy' => 'property-furnishing',
                'hide_empty' => 0,
            );
            $cats = get_categories($args);
            foreach( $cats as $cat ){
                if($cat->slug == $pro['furnishing']){
                    $selected = 'selected';
                }else{
                    $selected = '';
                }
                echo '<input type="checkbox" value="'.$cat->slug.'" '.$selected.'>'.$cat->name.'</input>';
            }
        ?>
        </form>

2 个答案:

答案 0 :(得分:1)

变化:

<select name="property-furnishing" class="submit-select">

为:

<select multiple name="property-furnishing" class="submit-select">

答案 1 :(得分:1)

要从<select>框中获取多条记录,您需要在multiple=""标记内使用multiple="multiple"<select>作为:

<select multiple="" name="property-furnishing[]" class="submit-select">

请注意:当您在框中使用多个属性时,您需要将名称字段用作property-furnishing[]之类的数组,您将获得SUPER GLOBAL (POST/GET)中所有选定的值在PHP