'tag-cloud', 'description' => _a("Your most used tags in cloud format")); $this->WP_Widget('atom-tag-cloud', _a('Tag Cloud'), $widget_ops); } // calculate the tag color based on tag importance, start & end gradient colors (props to konforce from StackOverflow) function get_tag_color($weight, $mincolor, $maxcolor){ $weight = $weight/100; $mincolor = hexdec($mincolor); $maxcolor = hexdec($maxcolor); $r1 = ($mincolor >> 16) & 0xff; $g1 = ($mincolor >> 8) & 0xff; $b1 = $mincolor & 0xff; $r2 = ($maxcolor >> 16) & 0xff; $g2 = ($maxcolor >> 8) & 0xff; $b2 = $maxcolor & 0xff; $r = $r1 + ($r2 - $r1) * $weight; $g = $g1 + ($g2 - $g1) * $weight; $b = $b1 + ($b2 - $b1) * $weight; return sprintf("%06x", (($r << 16) | ($g << 8) | $b)); } // almost the same as WP's function, only added color styles & removed irrelevant arguments function generate_tag_cloud($tags, $args = ''){ $defaults = array( 'smallest' => 8, 'largest' => 22, 'number' => 45, 'gradient_start' => false, 'gradient_end' => false, 'order' => 'ASC', 'topic_count_text_callback' => 'default_topic_count_text', 'topic_count_scale_callback' => 'default_topic_count_scale' ); if (!isset($args['topic_count_text_callback']) && isset($args['single_text']) && isset($args['multiple_text'])): $body = 'return sprintf(_n('.var_export($args['single_text'], true).', '.var_export($args['multiple_text'], true).', $count), number_format_i18n($count));'; $args['topic_count_text_callback'] = create_function('$count', $body); endif; $args = wp_parse_args($args, $defaults); extract($args); if (empty($tags)) return; $tags_sorted = apply_filters('tag_cloud_sort', $tags, $args); if ($tags_sorted != $tags) { // the tags have been sorted by a plugin $tags = $tags_sorted; unset($tags_sorted); } else { if ('RAND' == $order){ shuffle($tags); } else { // SQL cannot save you; this is a second (potentially different) sort on a subset of data. uasort($tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);')); if ('DESC' == $order) $tags = array_reverse($tags, true); } } if ($number > 0) $tags = array_slice($tags, 0, $number); $counts = array(); $real_counts = array(); // For the alt tag foreach ((array) $tags as $key => $tag): $real_counts[$key] = $tag->count; $counts[$key] = $topic_count_scale_callback($tag->count); endforeach; $min_count = min($counts); $spread = max($counts) - $min_count; if ($spread <= 0) $spread = 1; $font_spread = $largest - $smallest; if ($font_spread < 0) $font_spread = 1; $font_step = $font_spread / $spread; $a = array(); foreach ($tags as $key => $tag): $count = $counts[$key]; $real_count = $real_counts[$key]; if($gradient_start && $gradient_end): if ($largest == $smallest) $tag_weight = $largest; else $tag_weight = ($smallest+(($count-$min_count)*$font_step)); $diff = $largest-$smallest; if ($diff <= 0) $diff = 1; $color_weight = round(99*($tag_weight-$smallest)/($diff)+1); $tag_color = $this->get_tag_color($color_weight, $gradient_start, $gradient_end); endif; $tag_link = '#' != $tag->link ? esc_url($tag->link) : '#'; //$tag_id = isset($tags[$key]->id) ? $tags[$key]->id : $key; $size = ($smallest+(($count-$min_count)*$font_step)); $name = $tags[$key]->name; if($size > 15) $name = "{$name}"; // bold if size > 15pt $a[] = "slug}\" title=\"".esc_attr($topic_count_text_callback($real_count))."\" style=\"font-size:".sprintf("%01.1f", $size)."pt;".(isset($tag_color) ? "color:#{$tag_color};" : null)."\">{$name}"; endforeach; return apply_filters('wp_generate_tag_cloud', join("\n", $a), $tags, $args); } function tag_cloud($args = ''){ $defaults = array( 'smallest' => 8, 'largest' => 22, 'number' => 45, 'order' => 'ASC', // ASC/DESC/RAND -- @todo: maybe add a option for this? 'exclude' => '', 'include' => '', 'taxonomy' => 'post_tag'); $args = wp_parse_args($args, $defaults); $tags = get_terms($args['taxonomy'], array_merge($args, array('orderby' => 'count', 'order' => 'DESC'))); // Always query top tags if (empty($tags)) return; foreach ($tags as $key => $tag): $link = get_term_link(intval($tag->term_id), $args['taxonomy']); if (is_wp_error($link)) return false; $tags[$key]->link = $link; $tags[$key]->id = $tag->term_id; endforeach; $output = $this->generate_tag_cloud($tags, $args); // Here's where those top tags get sorted according to $args $output = apply_filters('wp_tag_cloud', $output, $args); return $output; } function widget($args, $instance){ extract($args); $instance = wp_parse_args($instance, $this->defaults()); $current_taxonomy = $this->_get_current_taxonomy($instance); $number = empty($instance['number']) ? 45 : $instance['number']; $smallest = empty($instance['smallest']) ? 8 : $instance['smallest']; $largest = empty($instance['largest']) ? 22 : $instance['largest']; $gradient_start = esc_attr($instance['gradient_start']); $gradient_end = esc_attr($instance['gradient_end']); $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); echo $before_widget; if ($title) echo $before_title.$title.$after_title; echo '
'; echo $this->tag_cloud(apply_filters('widget_tag_cloud_args', array( 'taxonomy' => $current_taxonomy, 'number' => $number, 'smallest' => $smallest, 'largest' => $largest, 'gradient_start' => $gradient_start, 'gradient_end' => $gradient_end ))); echo "
\n"; echo $after_widget; } function update($new_instance, $old_instance) { $instance['title'] = esc_attr($new_instance['title']); $instance['taxonomy'] = esc_attr($new_instance['taxonomy']); $instance['number'] = min(max(intval($new_instance['number']), 5), 1000); $instance['smallest'] = min(max(intval($new_instance['smallest']), 1), 100); $instance['largest'] = min(max(intval($new_instance['largest']), 1), 100); $instance['gradient_start'] = esc_attr($new_instance['gradient_start']); $instance['gradient_end'] = esc_attr($new_instance['gradient_end']); return wp_parse_args($instance, $this->defaults()); } function defaults(){ // default settings return apply_filters('atom_widget_tags_defaults', array( 'title' => _a("Tags"), 'number' => 45, 'smallest' => 8, 'largest' => 22, 'gradient_start' => 'cccccc', 'gradient_end' => '333333', ), $this); } function form($instance){ $instance = wp_parse_args($instance, $this->defaults()); $current_taxonomy = $this->_get_current_taxonomy($instance); ?>
>


pt

pt