Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When you create a custom taxonomy, seemingly the only options are to display it as a tag (non-hierarchical) or category (hierarchical). Is there a way to display them as a dropdown menu?

I realise that this an be done using add_meta_box() and actually manually adding it, but I'm looking for a builtin way (if one exists), to save lots of code! Thanks.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Unfortunately there is no way of doing this with wp_register_taxonomy(). When you register a taxonomy, the default metabox is either checkboxes (hierarchical) or as a cloud (non-hierarchical).

The only way round it is to de-register the existing metabox and add a new-one in its place. By carefully mimicking the 'default' metabox - you won't have to process the data yourself, but let WordPress automatically handle adding / removing terms.

The details of how to do this are given in this answer. You may also find this GitHub repository helpful (which now includes the ability of creating terms on the fly in the new metabox). The linked resources refer to creating a metabox with radio boxes, but it could be easily adapted for drop-down menus.

share|improve this answer
    
Thanks Stephen, that is as I feared! Would be great if WP implimented this in the future, would save lots of coding for people like me. Thanks again. –  David Gard Apr 25 '12 at 11:08

You can add new taxonomy with your own metabox since wp 3.8 by meta_box_cb

"drop_cat()" - consist of rebuild original core category metabox wp-admin/includes/meta-boxes.php

function realty_type() {
$args = array(
    'show_ui'                    => true,
    'meta_box_cb'                => 'drop_cat',
);
register_taxonomy( 'realty_type', array( 'YOUR_POST_TYPE' ), $args );

}

// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );


function drop_cat( $post, $box ) {
$defaults = array('taxonomy' => 'category');
if ( !isset($box['args']) || !is_array($box['args']) )
    $args = array();
else
    $args = $box['args'];
extract( wp_parse_args($args, $defaults), EXTR_SKIP );
$tax = get_taxonomy($taxonomy);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">

        <?php 
        $name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
        echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
        ?>
        <? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
        <ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
            <?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
        </ul>

        <?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '&mdash;' ) ); ?>

</div>
<?php
}
share|improve this answer

Here you go:

<select>
    <?php
       $tax_terms = get_terms('your_taxonomy_name', array('hide_empty' => '0'));      
       foreach ( $tax_terms as $tax_term ):  
          echo '<option value="'.$tax_term->name.'">'.$tax_term->name.'</option>';   
       endforeach;
    ?>
</select> 

Delete $tax_terms second argument if you don't want to display empty terms.

Also use selected if you want want to save picked option :)

share|improve this answer
    
Thanks @Wordpressor - I'm looking to do it without having to add any metaboxes to the edit post page if I can, so it was built in ways using the wp_register_taxonomy() function I was looking for. Will update my question! –  David Gard Apr 24 '12 at 13:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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