From 96b987270d7fcdeffffe5800f98d5ca589540cc8 Mon Sep 17 00:00:00 2001 From: ryan Date: Tue, 19 Aug 2008 16:36:18 +0000 Subject: [PATCH] Tag cloud fixes from DD32. Implements number arg. see #6015 git-svn-id: http://svn.automattic.com/wordpress/trunk@8668 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/category-template.php | 74 +++++++++++++++++-------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index dd88b5ec78..ec276af4a2 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -350,10 +350,16 @@ function wp_tag_cloud( $args = '' ) { if ( empty( $tags ) ) return; - $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args + foreach ( $tags as $key => $tag ) { + $link = get_tag_link( $tag->term_id ); + if ( is_wp_error( $link ) ) + return false; - if ( is_wp_error( $return ) ) - return false; + $tags[ $key ]->link = $link; + $tags[ $key ]->id = $tag->term_id; + } + + $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args $return = apply_filters( 'wp_tag_cloud', $return, $args ); @@ -377,23 +383,39 @@ function wp_tag_cloud( $args = '' ) { function wp_generate_tag_cloud( $tags, $args = '' ) { global $wp_rewrite; $defaults = array( - 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, - 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC' + 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0, + 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC', + 'single_text' => '%d topic', 'multiple_text' => '%d topics' ); $args = wp_parse_args( $args, $defaults ); extract( $args ); if ( empty( $tags ) ) return; - $counts = $tag_links = array(); - foreach ( (array) $tags as $tag ) { - $counts[$tag->name] = $tag->count; - $tag_links[$tag->name] = get_tag_link( $tag->term_id ); - if ( is_wp_error( $tag_links[$tag->name] ) ) - return $tag_links[$tag->name]; - $tag_ids[$tag->name] = $tag->term_id; + + // SQL cannot save you; this is a second (potentially different) sort on a subset of data. + if ( 'name' == $orderby ) + uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') ); + else + uasort( $tags, create_function('$a, $b', 'return ($a->count < $b->count);') ); + + if ( 'DESC' == $order ) + $tags = array_reverse( $tags, true ); + elseif ( 'RAND' == $order ) { + $keys = array_rand( $tags, count( $tags ) ); + foreach ( $keys as $key ) + $temp[$key] = $tags[$key]; + $tags = $temp; + unset( $temp ); } + if ( $number > 0 ) + $tags = array_slice($tags, 0, $number); + + $counts = array(); + foreach ( (array) $tags as $key => $tag ) + $counts[ $key ] = $tag->count; + $min_count = min( $counts ); $spread = max( $counts ) - $min_count; if ( $spread <= 0 ) @@ -403,32 +425,18 @@ function wp_generate_tag_cloud( $tags, $args = '' ) { $font_spread = 1; $font_step = $font_spread / $spread; - // SQL cannot save you; this is a second (potentially different) sort on a subset of data. - if ( 'name' == $orderby ) - uksort( $counts, 'strnatcasecmp' ); - else - asort( $counts ); - - if ( 'DESC' == $order ) - $counts = array_reverse( $counts, true ); - elseif ( 'RAND' == $order ) { - $keys = array_rand( $counts, count( $counts ) ); - foreach ( $keys as $key ) - $temp[$key] = $counts[$key]; - $counts = $temp; - unset( $temp ); - } - $a = array(); $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; - foreach ( $counts as $tag => $count ) { - $tag_id = $tag_ids[$tag]; - $tag_link = clean_url($tag_links[$tag]); - $a[] = "$tag"; + . "$unit;'>$tag_name"; } switch ( $format ) :