Created
January 27, 2012 18:26
-
-
Save wpsmith/1690166 to your computer and use it in GitHub Desktop.
Taxonomy and Terms Dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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