• Hi guys, πŸ™‚

    I’ve been using this code in the “Dropdown without a Submit Button using JavaScript (2)” section of that page:

    <?php
    $select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
    ?>

    I’ve used it in a few of my wordpress installations and every time it outputs two select fields with the categories inside. But only the second one has the required form onsubmit code: onchange=’return this.form.submit()’

    Does anyone know why it’s adding two category drop downs for me and is there any way I can stop it other than using some hacky CSS first-child display none? πŸ˜€

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Unit9

    (@unit9)

    Hi guys,

    I’ve come to the conclusion that

    wp_dropdown_categories()

    Is echoing to the front end of the site without me telling it to. And then echoing out again with the preg_replace onchange=’return this.form.submit()’ code included.

    Is there any way I can prevent wp_dropdown_categories() from echoing to the front-end in the first instance?

    Thanks! πŸ™‚

    hussong

    (@hussong)

    You can set up an array of arguments

    $args = array(
    	'show_option_all'    => false,
    	'show_option_none'   => ' ',
    	'orderby'            => $orderby,
    	'order'              => $ascdsc,
    	'show_count'         => $showcount,
    	'hide_empty'         => 1,
    	'child_of'           => $childof,
    	'exclude'            => $exclude,
    	'echo'               => 0,
    )

    and pass that to wp_dropdown_categories

    $select = wp_dropdown_categories($args);
    $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
    echo $select;
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Why is wp_dropdown_categories adding two select dropdowns?’ is closed to new replies.