One thing I dislike about WordPress taxonomies is that there is no built in way to limit a post to only having 1 term for a certain taxonomy. You always have to have a metabox with all the terms shown alongside checkboxes, and it allows you to check as many as you want.
Sometimes you might want to limit 1 term from a taxonomy. A really quick way to do that is with the following piece of code you can put in your functions file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | add_action( 'admin_footer', 'nk_limit_taxonomy', 10, 1 ); function nk_limit_taxonomy( $array ) { ?> <script> jQuery("input[name=\"tax_input[product_category][]\"]").click(function () { selected = jQuery("input[name=\"tax_input[product_category][]\"]").filter(":checked").length; if (selected > 1){ jQuery("input[name=\"tax_input[product_category][]\"]").each(function () { jQuery(this).attr("checked", false); }); jQuery(this).attr("checked", true); } }); </script> <?php } |
This effectively prevents the user from selecting more than 1 term on a post. It will unselect everything if you attempt to check another. It’s not foolproof, but for the casual user it should be adequate in what you need.