Convert dropdown into multi select check box in wordpress website

I am trying to add some custom coding in my website’s wordpress theme files.

Currently the following code displays the value in dropdown box, i want to convert it into multiselect check box

Read More
<!-- 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>

I tried using this. Is this fine ?

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

Related posts

2 comments

  1. Change:

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

    to:

    <select multiple name="property-furnishing" class="submit-select">
    
  2. For getting multiple records from a <select> box you need to use multiple="" or multiple="multiple" inside the <select> tag as:

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

    Note that: when you use multiple attribute in box you need to use name field as an array like property-furnishing[] and you will get the all selected values in your SUPER GLOBAL (POST/GET) in PHP.

Comments are closed.