-
-
Save agrogeek/26b404b13dd3e6074aaa495ee1c1fcec to your computer and use it in GitHub Desktop.
wp_dropdown_category multiple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_dropdown_categories( array( | |
'taxonomy' => 'category', | |
'multiple' => true, | |
'walker' => new Willy_Walker_CategoryDropdown(), | |
'selected' => array( 10, 12 ), // selected terms… | |
'hide_empty' => false, | |
) ); | |
/** | |
* pour un meilleur style, vous pouvez utiliser select2 | |
* … (et ajouter un attribut 'class' => 'select2-multiple' dans les arguments) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This filter allow a wp_dropdown_categories select to return multiple items | |
add_filter( 'wp_dropdown_cats', 'willy_wp_dropdown_cats_multiple', 10, 2 ); | |
function willy_wp_dropdown_cats_multiple( $output, $r ) { | |
if ( ! empty( $r['multiple'] ) ) { | |
$output = preg_replace( '/<select(.*?)>/i', '<select$1 multiple="multiple">', $output ); | |
$output = preg_replace( '/name=([\'"]{1})(.*?)\1/i', 'name=$2[]', $output ); | |
} | |
return $output; | |
} | |
// This Walker is needed to match more than one selected value | |
class Willy_Walker_CategoryDropdown extends Walker_CategoryDropdown { | |
public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { | |
$pad = str_repeat(' ', $depth * 3); | |
/** This filter is documented in wp-includes/category-template.php */ | |
$cat_name = apply_filters( 'list_cats', $category->name, $category ); | |
if ( isset( $args['value_field'] ) && isset( $category->{$args['value_field']} ) ) { | |
$value_field = $args['value_field']; | |
} else { | |
$value_field = 'term_id'; | |
} | |
$output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $category->{$value_field} ) . "\""; | |
// Type-juggling causes false matches, so we force everything to a string. | |
if ( in_array( $category->{$value_field}, (array)$args['selected'], true ) ) | |
$output .= ' selected="selected"'; | |
$output .= '>'; | |
$output .= $pad.$cat_name; | |
if ( $args['show_count'] ) | |
$output .= ' ('. number_format_i18n( $category->count ) .')'; | |
$output .= "</option>\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment