/**
Display tag cloud.
The text size is set by the 'smallest' and 'largest' arguments, which will
use the 'unit' argument value for the CSS text size unit. The 'format'
argument can be 'flat' (default), 'list', or 'array'. The flat value for the
'format' argument will separate tags with spaces. The list value for the
'format' argument will format the tags in a UL HTML list. The array value for
the 'format' argument will return in PHP array type format.
The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC'.
The 'number' argument is how many tags to return. By default, the limit will
be to return the top 45 tags in the tag cloud list.
The 'topic_count_text_callback' argument is a function, which, given the count
of the posts with that tag, returns a text for the tooltip of the tag link.
The 'exclude' and 'include' arguments are used for the {@ get_tags()}
function. Only one should be used, because only one will be used and the
other ignored, if they are both set.
@since 2.3.0
@ array|string $args Optional. Override default arguments.
@return array Generated tag cloud, only if no failures and 'array' is set for the 'format' argument.
*/
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => '', 'link' => 'view', 'taxonomy' => 'post_tag', 'echo' => true
);
$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 ) {
if ( 'edit' == $args['link'] )
$link = get_edit_tag_link( $tag->term_id, $args['taxonomy'] );
else
$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;
}
$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 );
if ( 'array' == $args['format'] || empty($args['echo']) )
return $return;
echo $return;
}
/**
Default text for tooltip for tag links
@ integer $count number of posts with that tag
@return string text for the tooltip of a tag link.
*/
function default_topic_count_text( $count ) {
return sprintf( _n('%s topic', '%s topics', $count), number_format_i18n( $count ) );
}
/**
Generates a tag cloud (heatmap) from provided data.
The text size is set by the 'smallest' and 'largest' arguments, which will
use the 'unit' argument value for the CSS text size unit. The 'format'
argument can be 'flat' (default), 'list', or 'array'. The flat value for the
'format' argument will separate tags with spaces. The list value for the
'format' argument will format the tags in a UL HTML list. The array value for
the 'format' argument will return in PHP array type format.
The 'tag_cloud_sort' filter allows you to override the sorting done
by the 'orderby' argument; passed to the filter: $tags array and $args array.
The 'orderby' argument will accept 'name' or 'count' and defaults to 'name'.
The 'order' is the direction to sort, defaults to 'ASC' and can be 'DESC' or
'RAND'.
The 'number' argument is how many tags to return. By default, the limit will
be to return the entire tag cloud list.
The 'topic_count_text_callback' argument is a function, which given the count
of the posts with that tag returns a text for the tooltip of the tag link.
@todo Complete functionality.
@since 2.3.0
@ array $tags List of tags.
@ string|array $args Optional, override default arguments.
@return string
*/
function wp_generate_tag_cloud( $tags, $args = '' ) {
global $wp_rewrite;
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 0,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'topic_count_text_callback' => 'default_topic_count_text',
'filter' => 1,
);
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);
}
$args = wp_parse_args( $args, $defaults );
extract( $args );
if ( empty( $tags ) )
return;
// 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);') );
$tags = apply_filters( 'tag_cloud_sort', $tags, $args );
if ( 'DESC' == $order )
$tags = array_reverse( $tags, true );
elseif ( 'RAND' == $order ) {
$keys = (array) array_rand( $tags, count( $tags ) );
$temp = array();
foreach ( $keys as $key )
$temp[$key] = $tags[$key];
$tags = $temp;
$temp = null;
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 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread < 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
$a = array();
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $tags as $key => $tag ) {
$count = $counts[ $key ];
$tag_link = '#' != $tag->link ? esc_url( $tag->link ) : '#';
$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='" . esc_attr( $topic_count_text_callback( $count ) ) . "'$rel style='font-size: " .
( $smallest + ( ( $count - $min_count ) * $font_step ) )
. "$unit;'>$tag_name</a>";
}
switch ( $format ) :
case 'array' :
$return =& $a;
break;
case 'list' :
$return = "<ul class='wp-tag-cloud'>\n\t<li>";
$return .= join( "</li>\n\t<li>", $a );
$return .= "</li>\n</ul>\n";
break;
default :
$return = join( "\n", $a );
break;
endswitch;
if ( $filter )
return apply_filters( 'wp_generate_tag_cloud', $return, $tags, $args );
else
return $return;
}
//
// Helper functions
//
/**
Retrieve HTML list content for category list.
@uses Walker_Category to create HTML list content.
@since 2.1.0
@ Walker_Category::walk() for parameters and return description.
/
function walk_category_tree() {
$args = func_get_args();
// the user's options are the third parameter
if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
$walker = new Walker_Category;
else
$walker = $args[2]['walker'];
return call_user_func_array(array( &$walker, 'walk' ), $args );
}
/**
Retrieve HTML dropdown (select) content for category list.
@uses Walker_CategoryDropdown to create HTML dropdown content.
@since 2.1.0
@ Walker_CategoryDropdown::walk() for parameters and return description.
/
function walk_category_dropdown_tree() {
$args = func_get_args();
// the user's options are the third parameter
if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
$walker = new Walker_CategoryDropdown;
else
$walker = $args[2]['walker'];
return call_user_func_array(array( &$walker, 'walk' ), $args );
}
//
// Tags
//
/**
Retrieve the link to the tag.
@since 2.3.0
@uses apply_filters() Calls 'tag_link' with tag link and tag ID as parameters.
@ int $tag_id Tag (term) ID.
@return string
/
function get_tag_link( $tag_id ) {
global $wp_rewrite;
$taglink = $wp_rewrite->get_tag_permastruct();
$tag = &get_term( $tag_id, 'post_tag' );
if ( is_wp_error( $tag ) )
return $tag;
$slug = $tag->slug;
if ( empty( $taglink ) ) {
$file = get_option( 'home' ) . '/';
$taglink = $file . '?tag=' . $slug;
} else {
$taglink = str_replace( '%tag%', $slug, $taglink );
$taglink = get_option( 'home' ) . user_trailingsla****( $taglink, 'category' );
}
return apply_filters( 'tag_link', $taglink, $tag_id );
}