Posted By

fjarrett on 09/26/10


Tagged

wordpress


Versions (?)

Custom Taxonomy Terms Dropdown Function


 / Published in: PHP
 

URL: http://snipplr.com/view/41042/custom-taxonomy-terms-dropdown-template-tag/

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.

  1. function dropdown_taxonomy_term( $taxonomy, $args = '' ) {
  2. $defaults = array(
  3. 'orderby' => 'name',
  4. 'order' => 'ASC',
  5. 'hide_empty' => 1,
  6. 'format' => 'flat',
  7. 'number' => 45,
  8. 'hide_empty' => 1,
  9. 'fields' => 'all',
  10. 'slug' => '',
  11. 'hierarchical' => 1,
  12. 'name__like' => '',
  13. 'pad_counts' => 0,
  14. 'get' => '',
  15. 'child_of' => 0,
  16. 'parent' => ''
  17. );
  18.  
  19. $args = wp_parse_args( $args, $defaults );
  20.  
  21. $terms = get_terms( $taxonomy, array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) );
  22.  
  23. if ( empty($terms) )
  24. return;
  25.  
  26. $return = dropdown_generate_taxonomy_term( $terms, $taxonomy, $args );
  27.  
  28. if ( is_wp_error( $return ) )
  29. return false;
  30. else
  31. echo apply_filters( 'dropdown_taxonomy_term', $return, $args );
  32. }
  33.  
  34.  
  35. function dropdown_generate_taxonomy_term( $terms, $taxonomy, $args = '' ) {
  36. global $wp_rewrite;
  37.  
  38. $defaults = array(
  39. 'orderby' => 'name',
  40. 'order' => 'ASC',
  41. 'hide_empty' => 1,
  42. 'format' => 'flat',
  43. 'number' => 45,
  44. 'hide_empty' => 1,
  45. 'fields' => 'all',
  46. 'slug' => '',
  47. 'hierarchical' => 1,
  48. 'name__like' => '',
  49. 'pad_counts' => 0,
  50. 'get' => '',
  51. 'child_of' => 0,
  52. 'parent' => ''
  53. );
  54.  
  55. $args = wp_parse_args( $args, $defaults );
  56.  
  57. extract($args);
  58.  
  59. if ( !$terms )
  60. return;
  61.  
  62. $counts = $term_links = array();
  63.  
  64. foreach ( (array) $terms as $term ) {
  65. $counts[$term->name] = $term->count;
  66. $term_links[$term->name] = get_term_link( $term->name, $taxonomy );
  67. if ( is_wp_error( $term_links[$term->name] ) )
  68. return $term_links[$term->name];
  69. $term_ids[$term->name] = $term->term_id;
  70. }
  71.  
  72. $min_count = min($counts);
  73. $spread = max($counts) - $min_count;
  74.  
  75. if ( $spread <= 0 )
  76. $spread = 1;
  77.  
  78. $font_spread = $largest - $smallest;
  79.  
  80. if ( $font_spread <= 0 )
  81. $font_spread = 1;
  82.  
  83. $font_step = $font_spread / $spread;
  84.  
  85. if ( 'name' == $orderby )
  86. uksort($counts, 'strnatcasecmp');
  87. else
  88. asort($counts);
  89.  
  90. if ( 'DESC' == $order )
  91. $counts = array_reverse( $counts, true );
  92. $a = array();
  93.  
  94. $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="term"' : '';
  95.  
  96. foreach ( $counts as $term => $count ) {
  97. $term_id = $term_ids[$term];
  98. $term_link = clean_url($term_links[$term]);
  99. $term = str_replace(' ', '&nbsp;', wp_specialchars( $term ));
  100. $a[] = "\t<option value='$term_link'>$term ($count)</option>";
  101. }
  102.  
  103. switch ( $format ) :
  104.  
  105. case 'array' :
  106. $return =& $a;
  107. break;
  108.  
  109. case 'list' :
  110. $return = "<ul class='wp-taxonomy-term'>\n\t<li>";
  111. $return .= join("</li>\n\t<li>", $a);
  112. $return .= "</li>\n</ul>\n";
  113. break;
  114.  
  115. default :
  116. $return = join("\n", $a);
  117. break;
  118.  
  119. endswitch;
  120.  
  121. return apply_filters( 'dropdown_generate_taxonomy_term', $return, $terms, $args );
  122. }

Report this snippet  

You need to login to post a comment.