Make WordPress Core


Ignore:
Timestamp:
04/10/2007 07:52:15 PM (20 years ago)
Author:
ryan
Message:

Tag clouds from mdawaffe. fixes #4129

File:
1 edited

Legend:

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

    r5231 r5234  
    310310}
    311311
     312function wp_tag_cloud( $args = '' ) {
     313        $defaults = array(
     314                'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
     315                'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
     316                'exclude' => '', 'include' => ''
     317        );
     318        $args = wp_parse_args( $args, $defaults );
     319
     320        $tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
     321
     322        if ( empty($tags) )
     323                return;
     324
     325        $return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
     326        echo apply_filters( 'wp_tag_cloud', $return, $args );
     327}
     328
     329// $tags = prefetched tag array ( get_tags() )
     330// $args['format'] = 'flat' => whitespace separated, 'list' => UL, 'array' => array()
     331// $args['orderby'] = 'name', 'count'
     332function wp_generate_tag_cloud( $tags, $args = '' ) {
     333        global $wp_rewrite;
     334        $defaults = array(
     335                'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
     336                'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
     337        );
     338        $args = wp_parse_args( $args, $defaults );
     339        extract($args);
     340
     341        if ( !$tags )
     342                return;
     343        $counts = $tag_links = array();
     344        foreach ( (array) $tags as $tag ) {
     345                $counts[$tag->cat_name] = $tag->tag_count;
     346                $tag_links[$tag->cat_name] = get_tag_link( $tag->cat_ID );
     347        }
     348
     349        $min_count = min($counts);
     350        $spread = max($counts) - $min_count;
     351        if ( $spread <= 0 )
     352                $spread = 1;
     353        $font_spread = $largest - $smallest;
     354        if ( $font_spread <= 0 )
     355                $font_spread = 1;
     356        $font_step = $font_spread / $spread;
     357
     358        // SQL cannot save you; this is a second (potentially different) sort on a subset of data.
     359        if ( 'name' == $orderby )
     360                uksort($counts, 'strnatcasecmp');
     361        else
     362                asort($counts);
     363
     364        if ( 'DESC' == $order )
     365                $counts = array_reverse( $tag_counts, true );
     366
     367        $a = array();
     368
     369        $rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
     370
     371        foreach ( $counts as $tag => $count ) {
     372                $tag_link = clean_url($tag_links[$tag]);
     373                $tag = str_replace(' ', '&nbsp;', wp_specialchars( $tag ));
     374                $a[] = "<a href='$tag_link' title='" . attribute_escape( sprintf( __('%d topics'), $count ) ) . "'$rel style='font-size: " .
     375                        ( $smallest + ( ( $count - $min_count ) * $font_step ) )
     376                        . "$unit;'>$tag</a>";
     377        }
     378
     379        switch ( $format ) :
     380        case 'array' :
     381                $return =& $a;
     382                break;
     383        case 'list' :
     384                $return = "<ul class='wp-tag-cloud'>\n\t<li>";
     385                $return .= join("</li>\n\t<li>", $a);
     386                $return .= "</li>\n</ul>\n";
     387                break;
     388        default :
     389                $return = join("\n", $a);
     390                break;
     391        endswitch;
     392
     393        return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
     394}
     395
    312396//
    313397// Helper functions
Note: See TracChangeset for help on using the changeset viewer.