How to mark the category as selected in wp_dropdown_categories() wordpress


I'm displaying a drop down list of categories on a front end form using this code. I want the category which is set in the post to be selected when the form loads but this isn't working. What wrong with the code?

Posts category as selected in wp_dropdown_categories (4 posts)

$category = get_the_category();
$selected = 0;
if ( $category ) {
$selected = $category[0]->cat_name;
wp_dropdown_categories( 'hierarchical=1&hide_empty=0&orderby=name&id=cat&show_count=0&title_li=&use_desc_for_title=1&class=cat requiredField&selected=' . $selected );

The selected argument of the wp_dropdown_categories needs to be an integer (i.e. the id of the category).
Try changing this: $selected = $category[0]->cat_name;
to this: $selected = $category[0]->cat_ID;
Just following up. Did that resolve your issue?
Yes thank you

fuente 1, fuente 2

*English post

0 comentarios:

Publicar un comentario