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
This commit is contained in:
ryan 2008-08-19 16:36:18 +00:00
parent f4f6fb2019
commit 96b987270d
1 changed files with 41 additions and 33 deletions

View File

@ -350,11 +350,17 @@ function wp_tag_cloud( $args = '' ) {
if ( empty( $tags ) ) if ( empty( $tags ) )
return; 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( $return ) ) if ( is_wp_error( $link ) )
return false; 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 ); $return = apply_filters( 'wp_tag_cloud', $return, $args );
if ( 'array' == $args['format'] ) if ( 'array' == $args['format'] )
@ -377,23 +383,39 @@ function wp_tag_cloud( $args = '' ) {
function wp_generate_tag_cloud( $tags, $args = '' ) { function wp_generate_tag_cloud( $tags, $args = '' ) {
global $wp_rewrite; global $wp_rewrite;
$defaults = array( $defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45, 'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC' 'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'single_text' => '%d topic', 'multiple_text' => '%d topics'
); );
$args = wp_parse_args( $args, $defaults ); $args = wp_parse_args( $args, $defaults );
extract( $args ); extract( $args );
if ( empty( $tags ) ) if ( empty( $tags ) )
return; return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) { // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
$counts[$tag->name] = $tag->count; if ( 'name' == $orderby )
$tag_links[$tag->name] = get_tag_link( $tag->term_id ); uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
if ( is_wp_error( $tag_links[$tag->name] ) ) else
return $tag_links[$tag->name]; uasort( $tags, create_function('$a, $b', 'return ($a->count < $b->count);') );
$tag_ids[$tag->name] = $tag->term_id;
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 ); $min_count = min( $counts );
$spread = max( $counts ) - $min_count; $spread = max( $counts ) - $min_count;
if ( $spread <= 0 ) if ( $spread <= 0 )
@ -403,32 +425,18 @@ function wp_generate_tag_cloud( $tags, $args = '' ) {
$font_spread = 1; $font_spread = 1;
$font_step = $font_spread / $spread; $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(); $a = array();
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : ''; $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $counts as $tag => $count ) { foreach ( $tags as $key => $tag ) {
$tag_id = $tag_ids[$tag]; $count = $counts[ $key ];
$tag_link = clean_url($tag_links[$tag]); $tag_link = clean_url( $tag->link );
$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext('%d topic','%d topics',$count), $count ) ) . "'$rel style='font-size: " . $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
$tag_name = $tags[ $key ]->name;
$a[] = "<a href='$tag_link' class='tag-link-$tag_id' title='" . attribute_escape( sprintf( __ngettext( $single_text, $multiple_text, $count ), $count ) ) . "'$rel style='font-size: " .
( $smallest + ( ( $count - $min_count ) * $font_step ) ) ( $smallest + ( ( $count - $min_count ) * $font_step ) )
. "$unit;'>$tag</a>"; . "$unit;'>$tag_name</a>";
} }
switch ( $format ) : switch ( $format ) :