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.
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.
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
Here you go:
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 🙂
I was having difficulty with the code provided by Alexufo, so I rewrote it based on the updated function in WP 4.1.2, Here’s my updated drop_cat function:
The one issue I’m having (as indicated by my comments in the above function), is that I cannot use the WP core add term/category feature in the meta box, because it defaults to dynamically adding new terms as a checkbox above my select menu. In order to work with a select menu, the menu will need to be re-rendered after the new term is added. Something I’m sure I could figure out but will leave for another day.
the above code have some issues on me, let me put the right code that worked for me
your category definition , then put this code in here
remember to put