There’s a few things wrong with your code here.
- Your call to the function has the parameters mixed up.
$args
are passed first to the function but in your function definition they are expected to be passed second. - The typo that you’ve fixed of
$argo
to$args
- In your functions
foreach
loop you’re generating a lot of things that you just aren’t using.
Here I’ve simplified your code to only take 1 taxonomy instead of multiple taxonomies. It calls wp_parse_args()
so you can pass nothing to the $args
parameter or anything you want to override the default array. The foreach
loop you only need the slug and title as far as I can tell. Finally we wrap the loop with a conditional check just to make sure we have terms to loop through:
function get_terms_dropdown_tm( $taxonomy, $args = array() ) {
$args = wp_parse_args( $args, array(
'orderby' => 'name',
'show_count' => 1,
'order' => 'DESC',
'hide_empty' => 1
) );
$myterms = get_terms( $taxonomy, $args );
$output = sprintf( '<select name="%1$s">', esc_attr( sanitize_text_field( $taxonomy ) ) );
$output .= "<option value="#">Seleziona la marca</option>";
if( ! is_wp_error( $myterms ) ) {
foreach( $myterms as $term ) {
$output .= sprintf( '<option value="%1$s">%2$s</option>', $term->slug, $term->name );
}
}
$output .= "</select>";
return $output;
}
The call:
$taxonomy = 'tassonomia_marca';
$select = get_terms_dropdown_tm( $taxonomy );
$select = preg_replace( "#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $select );
echo $select;