Wordpress - Displaying Your Tags in a Dropdown Menu


/ Published in: PHP
Save to your folder(s)

From Web Design Ledger "13 Useful Code Snippets for WordPress Development"


Copy this code and paste it in your HTML
  1. <?php
  2. function dropdown_tag_cloud( $args = '' ) {
  3. $defaults = array(
  4. 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  5. 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
  6. 'exclude' => '', 'include' => ''
  7. );
  8. $args = wp_parse_args( $args, $defaults );
  9.  
  10. $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
  11.  
  12. if ( empty($tags) )
  13. return;
  14.  
  15. $return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
  16. if ( is_wp_error( $return ) )
  17. return false;
  18. else
  19. echo apply_filters( 'dropdown_tag_cloud', $return, $args );
  20. }
  21.  
  22. function dropdown_generate_tag_cloud( $tags, $args = '' ) {
  23. global $wp_rewrite;
  24. $defaults = array(
  25. 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
  26. 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
  27. );
  28. $args = wp_parse_args( $args, $defaults );
  29. extract($args);
  30.  
  31. if ( !$tags )
  32. return;
  33. $counts = $tag_links = array();
  34. foreach ( (array) $tags as $tag ) {
  35. $counts[$tag->name] = $tag->count;
  36. $tag_links[$tag->name] = get_tag_link( $tag->term_id );
  37. if ( is_wp_error( $tag_links[$tag->name] ) )
  38. return $tag_links[$tag->name];
  39. $tag_ids[$tag->name] = $tag->term_id;
  40. }
  41.  
  42. $min_count = min($counts);
  43. $spread = max($counts) - $min_count;
  44. if ( $spread <= 0 )
  45. $spread = 1;
  46. $font_spread = $largest - $smallest;
  47. if ( $font_spread <= 0 )
  48. $font_spread = 1;
  49. $font_step = $font_spread / $spread;
  50.  
  51. // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  52. if ( 'name' == $orderby )
  53. uksort($counts, 'strnatcasecmp');
  54. else
  55. asort($counts);
  56.  
  57. if ( 'DESC' == $order )
  58. $counts = array_reverse( $counts, true );
  59.  
  60. $a = array();
  61.  
  62. $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
  63.  
  64. foreach ( $counts as $tag => $count ) {
  65. $tag_id = $tag_ids[$tag];
  66. $tag_link = clean_url($tag_links[$tag]);
  67. $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
  68. $a[] = "\t<option value='$tag_link'>$tag ($count)</option>";
  69. }
  70.  
  71. switch ( $format ) :
  72. case 'array' :
  73. $return =& $a;
  74. break;
  75. case 'list' :
  76. $return = "<ul class='wp-tag-cloud'>\n\t<li>";
  77. $return .= join("</li>\n\t<li>", $a);
  78. $return .= "</li>\n</ul>\n";
  79. break;
  80. default :
  81. $return = join("\n", $a);
  82. break;
  83. endswitch;
  84.  
  85. return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
  86. }
  87. ?>
  88.  
  89. /*
  90. "Now, to finalize your dropdown menu you have to open the theme file where you want the list to be displayed (i.e. sidebar.php) and insert the following code:"
  91.  
  92. <select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
  93. <option value="#">Liste d'auteurs</option>
  94. <?php dropdown_tag_cloud('number=0&order=asc'); ?>
  95. </select>
  96. */

URL: http://webdesignledger.com/tips/13-useful-code-snippets-for-wordpress-development

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.