Skip to content

Instantly share code, notes, and snippets.

@JuanRangel
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuanRangel/0934cbc3dec20e3b1ce8 to your computer and use it in GitHub Desktop.
Save JuanRangel/0934cbc3dec20e3b1ce8 to your computer and use it in GitHub Desktop.
Get all terms from a taxonomy and use a select options
function get_tax_options( $taxonomy, $field ) {
$terms = get_terms( $taxonomy );
$current = ( isset($_POST[$field]) ) ? $_POST[$field] : '';
$options = array();
$options[] = '<option value="">-- ' . ucwords( $taxonomy ) .' --</option>';
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
$options[] = sprintf( '<option value="%s" %s>%s</option>', $term->slug ,selected( $current, $term->slug ), $term->name );
}
}
return implode( ' ', $options );;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment