Skip to content

Instantly share code, notes, and snippets.

@wpsmith
Created January 27, 2012 18:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpsmith/1690166 to your computer and use it in GitHub Desktop.
Save wpsmith/1690166 to your computer and use it in GitHub Desktop.
Taxonomy and Terms Dropdown
add_action( 'ntg_render_taxonomy_terms_select' , 'wps_taxonomy_terms_select' , 10 , 3 );
function wps_taxonomy_terms_select ( $field, $meta, $id ) {
global $post;
if ( $field['type'] == 'taxonomy_terms_select' ) {
echo '<select name="', $id, '" id="', $id, '" style="margin-top: 5px;">';
$pts = get_post_types();
echo '<option style="padding-right:10px;" value=""', selected ( $meta, '' ), '>', __( 'All Taxonomies and Terms', WPS_SLIDER ).'</option>';
if ( $field['options'] ) {
foreach ($field['options'] as $option) {
echo '<option value="', $option['value'], '"', $meta == $option['value'] ? ' selected="selected"' : '', '>', $option['name'], '</option>';
}
}
$taxonomies = get_taxonomies( array( 'public' => true ), 'objects' );
foreach ( $taxonomies as $taxonomy ) {
$query_label = '';
if ( !empty( $taxonomy->query_var ) )
$query_label = $taxonomy->query_var;
else
$query_label = $taxonomy->name;
echo '<optgroup label="', esc_attr( $taxonomy->labels->name ), '">';
echo '<option style="margin-left: 5px; padding-right:10px;" value="', esc_attr( $query_label ), '"', selected( esc_attr( $query_label ), $meta ), '>', $taxonomy->labels->all_items, '</option>';
$terms = get_terms( $taxonomy->name, 'orderby=name&hide_empty=1' );
foreach ( $terms as $term ) {
echo '<option style="margin-left: 8px; padding-right:10px;" value="', esc_attr( $query_label ) . ',' . $term->slug, '"', selected( esc_attr( $query_label ) . ',' . $term->slug, $meta ),'>', '-', esc_attr( $term->name ), '</option>';
}
echo '</optgroup>';
}
echo '</select>';
echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment