WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] select list of CUSTOM TAXONOMY with the option multiple (8 posts)

  1. mcfmullen
    Member
    Posted 1 year ago #

    I've been wondering forever how to get a multi-select dropdown of my categories and finally found my answer here in the form of:

    <?php $select_cats = wp_dropdown_categories( array( 'echo' => 0 ) );
    $select_cats = str_replace( "name='cat' id=", "name='cat[]' multiple='multiple' id=", $select_cats );
    echo $select_cats; ?>

    Which is AMAZING. However, I need it to go one step further and have a multi-select dropdown of a custom taxonomy called regions.

    How can I accomplish this?

  2. bcworkz
    Member
    Posted 1 year ago #

    Just specify your own taxonomy as one of the arguments in the passed array. It defaults to category, but can be overridden.

    array( 'echo' => 0,
      'taxonomy' => 'regions' )

    Depending on the taxonomy, other arguments may be needed, like if it's hierarchical. You can add any arguments that would be accepted by get_terms(), which is how wp_dropdown_categories() retrieves data from the database.

  3. mcfmullen
    Member
    Posted 1 year ago #

    Glad to know it is that simple!

  4. mcfmullen
    Member
    Posted 1 year ago #

    I now discovered a need to take this one step further. I required the name of the input field to be in this format:

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

    Given that my code looks like this:

    <?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
    	);
    	?>

    How do I do it?

  5. bcworkz
    Member
    Posted 1 year ago #

    Do you mean the <select> tag for drop down lists? You would add
    'name' => "scrape[$inpCnt][sponsors]",
    to the array definition. But if scrape is a javascript array, this will not work unless the whole thing is being echoed into a script block.

  6. mcfmullen
    Member
    Posted 1 year ago #

    it is a php array.

  7. bcworkz
    Member
    Posted 1 year ago #

    Not PHP without a $ in front, nor inside quotes. If the 'name' element is to be assigned a value in the array $scrape it should look like this:
    'name' => $scrape[$inpCnt]['sponsors'],

  8. mcfmullen
    Member
    Posted 1 year ago #

    you were correct the first time, thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic