Make WordPress Core

Changeset 8668


Ignore:
Timestamp:
08/19/2008 04:36:18 PM (16 years ago)
Author:
ryan
Message:

Tag cloud fixes from DD32. Implements number arg. see #6015

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/category-template.php

    r8666 r8668  
    351351        return;
    352352
     353    foreach ( $tags as $key => $tag ) {
     354        $link = get_tag_link( $tag->term_id );
     355        if ( is_wp_error( $link ) )
     356            return false;
     357
     358        $tags[ $key ]->link = $link;
     359        $tags[ $key ]->id = $tag->term_id;
     360    }
     361
    353362    $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
    354 
    355     if ( is_wp_error( $return ) )
    356         return false;
    357363
    358364    $return = apply_filters( 'wp_tag_cloud', $return, $args );
     
    378384    global $wp_rewrite;
    379385    $defaults = array(
    380         'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
    381         'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
     386        'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
     387        'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
     388        'single_text' => '%d topic', 'multiple_text' => '%d topics'
    382389    );
    383390    $args = wp_parse_args( $args, $defaults );
     
    386393    if ( empty( $tags ) )
    387394        return;
    388     $counts = $tag_links = array();
    389     foreach ( (array) $tags as $tag ) {
    390         $counts[$tag->name] = $tag->count;
    391         $tag_links[$tag->name] = get_tag_link( $tag->term_id );
    392         if ( is_wp_error( $tag_links[$tag->name] ) )
    393             return $tag_links[$tag->name];
    394         $tag_ids[$tag->name] = $tag->term_id;
    395     }
     395
     396    // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
     397    if ( 'name' == $orderby )
     398        uasort( $tags, create_function('$a, $b', 'return strnatcasecmp($a->name, $b->name);') );
     399    else
     400        uasort( $tags, create_function('$a, $b', 'return ($a->count < $b->count);') );
     401
     402    if ( 'DESC' == $order )
     403        $tags = array_reverse( $tags, true );
     404    elseif ( 'RAND' == $order ) {
     405        $keys = array_rand( $tags, count( $tags ) );
     406        foreach ( $keys as $key )
     407            $temp[$key] = $tags[$key];
     408        $tags = $temp;
     409        unset( $temp );
     410    }
     411
     412    if ( $number > 0 )
     413        $tags = array_slice($tags, 0, $number);
     414
     415    $counts = array();
     416    foreach ( (array) $tags as $key => $tag )
     417        $counts[ $key ] = $tag->count;
    396418
    397419    $min_count = min( $counts );
     
    404426    $font_step = $font_spread / $spread;
    405427
    406     // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
    407     if ( 'name' == $orderby )
    408         uksort( $counts, 'strnatcasecmp' );
    409     else
    410         asort( $counts );
    411 
    412     if ( 'DESC' == $order )
    413         $counts = array_reverse( $counts, true );
    414     elseif ( 'RAND' == $order ) {
    415         $keys = array_rand( $counts, count( $counts ) );
    416         foreach ( $keys as $key )
    417             $temp[$key] = $counts[$key];
    418         $counts = $temp;
    419         unset( $temp );
    420     }
    421 
    422428    $a = array();
    423429
    424430    $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
    425431
    426     foreach ( $counts as $tag => $count ) {
    427         $tag_id = $tag_ids[$tag];
    428         $tag_link = clean_url($tag_links[$tag]);
    429         $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: " .
     432    foreach ( $tags as $key => $tag ) {
     433        $count = $counts[ $key ];
     434        $tag_link = clean_url( $tag->link );
     435        $tag_id = isset($tags[ $key ]->id) ? $tags[ $key ]->id : $key;
     436        $tag_name = $tags[ $key ]->name;
     437        $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: " .
    430438            ( $smallest + ( ( $count - $min_count ) * $font_step ) )
    431             . "$unit;'>$tag</a>";
     439            . "$unit;'>$tag_name</a>";
    432440    }
    433441
Note: See TracChangeset for help on using the changeset viewer.