Thanks to Stack Exchange user MahdiY!
See original here.
In functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/** * Allow for multiple selects when using wp_dropdown */ add_filter( 'wp_dropdown_cats', 'wp_dropdown_cats_multiple', 10, 2 ); function wp_dropdown_cats_multiple( $output, $r ) { if( isset( $r['multiple'] ) && $r['multiple'] ) { $output = preg_replace( '/^<select/i', '<select multiple', $output ); $output = str_replace( "name='{$r['name']}'", "name='{$r['name']}[]'", $output ); foreach ( array_map( 'trim', explode( ",", $r['selected'] ) ) as $value ) $output = str_replace( "value=\"{$value}\"", "value=\"{$value}\" selected", $output ); } return $output; } |
When using wp_dropdown_categories just add the “multiple” argument:
1 2 3 4 5 6 7 8 |
$cat_args = array( 'orderby' => 'name', 'id' => 'cat-select', 'name' => 'cats', 'selected' => $_GET['cats']? implode(',', $_GET['cats']):'', 'multiple' => true ); wp_dropdown_categories( $cat_args ); |