0

I'm using this code to get a dropdown list of categories, but all options are marked as selected. Therefore, the last option is the one that is displayed as selected by default.

<?php wp_dropdown_categories( 
    array( 
        'class' => 'form-control', 
        'show_option_none' => 'Search by category...',
        'hierarchical'     => true,
        'depth'            => 1,
        'hide_empty'       => true,
        'exclude'          => 1,
        'orderby'          => 'NAME',
        'value_field'      => 'slug',
        )
    ); ?>

I also tried with the following parameter, but it didn't work.

'select' => 0

How can I prevent the selected attribute to be assigned to all items?

0

Add this parameter:

'select' => false

The solution is in the Wordpress Codex Function Reference for wp_dropdown_categories:

The default value for the selected parameter is 0, but this can cause a dropdown with all terms marked as selected. To fix this, set the selected parameter to false, rather than 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.