Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

wp_generate_tag_cloud

By: a guest on Apr 7th, 2012  |  syntax: None  |  size: 2.88 KB  |  views: 39  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function wp_generate_tag_cloud( $tags, $args = '' ) {
  2.         global $wp_rewrite;
  3.         $defaults = array(
  4.                 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
  5.                 'format' => 'flat', 'separator' => "\n", 'orderby' => 'name', 'order' => 'ASC',
  6.                 'topic_count_text_callback' => 'default_topic_count_text',
  7.                 'topic_count_scale_callback' => 'default_topic_count_scale', 'filter' => 1,
  8.         );
  9.  
  10.         if ( !isset( $args['topic_count_text_callback'] ) && isset( $args['single_text'] ) && isset( $args['multiple_text'] ) ) {
  11.                 $body = 'return sprintf (
  12.                         _n(' . var_export($args['single_text'], true) . ', ' . var_export($args['multiple_text'], true) . ', $count),
  13.                         number_format_i18n( $count ));';
  14.                 $args['topic_count_text_callback'] = create_function('$count', $body);
  15.         }
  16.  
  17.         $args = wp_parse_args( $args, $defaults );
  18.         extract( $args );
  19.  
  20.         if ( empty( $tags ) )
  21.                 return;
  22.  
  23.         $tags_sorted = apply_filters( 'tag_cloud_sort', $tags, $args );
  24.         if ( $tags_sorted != $tags  ) { // the tags have been sorted by a plugin
  25.                 $tags = $tags_sorted;
  26.                 unset($tags_sorted);
  27.         } else {
  28.                 if ( 'RAND' == $order ) {
  29.                         shuffle($tags);
  30.                 } else {
  31.                         // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
  32.                         if ( 'name' == $orderby )
  33.                                 uasort( $tags, '_wp_object_name_sort_cb' );
  34.                         else
  35.                                 uasort( $tags, '_wp_object_count_sort_cb' );
  36.  
  37.                         if ( 'DESC' == $order )
  38.                                 $tags = array_reverse( $tags, true );
  39.                 }
  40.         }
  41.  
  42.         if ( $number > 0 )
  43.                 $tags = array_slice($tags, 0, $number);
  44.  
  45.         $counts = array();
  46.         $real_counts = array(); // For the alt tag
  47.         foreach ( (array) $tags as $key => $tag ) {
  48.                 $real_counts[ $key ] = $tag->count;
  49.                 $counts[ $key ] = $topic_count_scale_callback($tag->count);
  50.         }
  51.  
  52.         $min_count = min( $counts );
  53.         $spread = max( $counts ) - $min_count;
  54.         if ( $spread <= 0 )
  55.                 $spread = 1;
  56.         $font_spread = $largest - $smallest;
  57.         if ( $font_spread < 0 )
  58.                 $font_spread = 1;
  59.         $font_step = $font_spread / $spread;
  60.  
  61.         $a = array();
  62.  
  63.         foreach ( $tags as $key => $tag ) {
  64.                 $count = $counts[ $key ];
  65.                 $real_count = $real_counts[ $key ];
  66.                 $tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
  67.                 $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
  68.                 $tag_name = $tags[ $key ]->name;
  69.                 $a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . esc_attr( call_user_func( $topic_count_text_callback, $real_count ) ) . "' style='font-size: " .
  70.                         ( $smallest + ( ( $count - $min_count ) * $font_step ) )
  71.                         . "$unit;'>$tag_name</a>";
  72.         }
  73.  
  74.         switch ( $format ) :
  75.         case 'array' :
  76.                 $return =& $a;
  77.                 break;
  78.         case 'list' :
  79.                 $return = "<ul class='wp-tag-cloud'>\n\t<li>";
  80.                 $return .= join( "</li>\n\t<li>", $a );
  81.                 $return .= "</li>\n</ul>\n";
  82.                 break;
  83.         default :
  84.                 $return = join( $separator, $a );
  85.                 break;
  86.         endswitch;
  87.  
  88.         if ( $filter )
  89.                 return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
  90.         else
  91.                 return $return;
  92. }
clone this paste RAW Paste Data