vendredi 10 février 2017

WordPress: How do I add item in dropdown with wp_dropdown_categories with empty string value

Vote count: 0

I am using wp_dropdown_categories in one of my custom forms. I want to add an additional item at top of the list with an empty string so that it renders like:

<select id="cmd" name="cmb" required>
   <option value=''>Select your language</option>
   <option value='2'>English</option>
   <option value='3'>Spanish</option>
   <option value='4'>Italian</option>
   <option value='5'>German</option>
</select>

This will allow me to use the native required attribute of HTML5 and the validation will fire automatically. I tried show_options_all already but the value of for this option comes up 0, which won't validate by required attribute.

<?php $args = array(
    'show_option_all'    => 'Select one',
    'show_option_none'   => '',
    'option_none_value'  => '-1',
    'orderby'            => 'Name',
    'order'              => 'ASC',
    'taxonomy'           => 'category',
    'hide_if_empty'      => false,
    'value_field'        => 'term_id',
); ?>

<?php wp_dropdown_categories( $args ); ?>

Is there a pure WordPress way to accomplish this?

asked 44 secs ago

Let's block ads! (Why?)



WordPress: How do I add item in dropdown with wp_dropdown_categories with empty string value

Aucun commentaire:

Enregistrer un commentaire