/ Published in: PHP
The dropdown taxonomy term function will enable you to show custom taxonomies in a dropdown menu. It supports both hierarchal (category-type) and non-hierarchal (tag-type) taxonomies.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function dropdown_taxonomy_term( $taxonomy, $args = '' ) { 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'format' => 'flat', 'number' => 45, 'hide_empty' => 1, 'fields' => 'all', 'slug' => '', 'hierarchical' => 1, 'name__like' => '', 'pad_counts' => 0, 'get' => '', 'child_of' => 0, 'parent' => '' ); $args = wp_parse_args( $args, $defaults ); $terms = get_terms( $taxonomy, array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); return; $return = dropdown_generate_taxonomy_term( $terms, $taxonomy, $args ); if ( is_wp_error( $return ) ) return false; else echo apply_filters( 'dropdown_taxonomy_term', $return, $args ); } function dropdown_generate_taxonomy_term( $terms, $taxonomy, $args = '' ) { global $wp_rewrite; 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 1, 'format' => 'flat', 'number' => 45, 'hide_empty' => 1, 'fields' => 'all', 'slug' => '', 'hierarchical' => 1, 'name__like' => '', 'pad_counts' => 0, 'get' => '', 'child_of' => 0, 'parent' => '' ); $args = wp_parse_args( $args, $defaults ); if ( !$terms ) return; $term_links[$term->name] = get_term_link( $term->name, $taxonomy ); if ( is_wp_error( $term_links[$term->name] ) ) return $term_links[$term->name]; $term_ids[$term->name] = $term->term_id; } if ( $spread <= 0 ) $spread = 1; $font_spread = $largest - $smallest; if ( $font_spread <= 0 ) $font_spread = 1; $font_step = $font_spread / $spread; if ( 'name' == $orderby ) else if ( 'DESC' == $order ) foreach ( $counts as $term => $count ) { $term_id = $term_ids[$term]; $term_link = clean_url($term_links[$term]); $a[] = "\t<option value='$term_link'>$term ($count)</option>"; } switch ( $format ) : case 'array' : $return =& $a; break; case 'list' : $return = "<ul class='wp-taxonomy-term'>\n\t<li>"; $return .= "</li>\n</ul>\n"; break; default : break; endswitch; return apply_filters( 'dropdown_generate_taxonomy_term', $return, $terms, $args ); }
URL: http://snipplr.com/view/41042/custom-taxonomy-terms-dropdown-template-tag/