0

I've discovered a workaround to get wp_category_dropdown to display as a multiple select field. My code is as follows:

<?php
$sponsors = wp_dropdown_categories( array(
    'child_of' => 0,
    'class' => 'postform',
    'depth' => 0,
    'echo' => 0,
    'exclude' => '',
    'hide_empty' => false,
    'hide_if_empty' => false,
    'hierarchical' => true,
    'order' => 'ASC',
    'orderby' => 'name',
    'selected' => 0,
    'show_count' => 0,
    'show_option_all' => '',
    'show_option_none' => __(''),
    'tab_index' => 0,
    'taxonomy' => 'sponsors', )
    );

$sponsors = str_replace(
    "name='cat' id=",
    "name='cat[]' multiple='multiple' size='19' id=",
    $sponsors
);
?>

So far, so good. Problem is, the above outputs the input name as 'cat' which is fine and dandy unless I need to loop through the above several times and with a more defining name.

As such, I need to have the name of the input be in the following format:

name="scrape[<?php echo $inpCnt; ?>][sponsors]"

However, doing this doesn't work:

$sponsors = str_replace(
    "name='scrape['.$inpCnt.'][sponsors]' id=",
    "name='scrape['.$inpCnt.'][sponsors][]' multiple='multiple' size='19' id=",
    $sponsors
);

So what can I do?

2

The dot . in your code is evaluated as string litral, and not concatenation. Meaning if $inpCnt was equal to 5 for example, php will search for name='scrape['.5.'][sponsors]' id= and will fail to find it. You can either remove the dots while still using the double quotes:

$sponsors = str_replace(
    "name='scrape['$inpCnt'][sponsors]' id=",
    "name='scrape['$inpCnt'][sponsors][]' multiple='multiple' size='19' id=",
    $sponsors
);

.. Or use concatenation:

$sponsors = str_replace(
    'name=\'scrape["'.$inpCnt.'"][sponsors]\' id=',
    'name=\'scrape["'.$inpCnt.'"][sponsors][]\' multiple=\'multiple\' size=\'19\' id=',
    $sponsors
);
  • Duly noted. With that said, it still outputs 'cat' as the input name. – Sweepster Jan 28 '13 at 0:06

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.