Skip to content

Instantly share code, notes, and snippets.

@kasparsd
Last active December 30, 2015 12:09
Show Gist options
  • Save kasparsd/7826812 to your computer and use it in GitHub Desktop.
Save kasparsd/7826812 to your computer and use it in GitHub Desktop.
Add tag counts as classname in the $range of 1 to 4 depending of the post count in each category
<?php
add_filter( 'wp_generate_tag_cloud', 'maybe_color_tag_cloud', 10, 3 );
function maybe_color_tag_cloud( $cloud, $tags, $args ) {
$a = array();
$counts = array();
$range = 4;
// Get term counts for our math needs
foreach ( $tags as $tag )
$counts[ $tag->term_id ] = $tag->count;
$count_min = min( $counts );
$count_unit = ( max( $counts ) - min( $counts ) ) / $range;
$font_unit = ( max( $counts ) - min( $counts ) ) / ( $args['largest'] - $args['smallest'] );
foreach ( $tags as $key => $tag ) {
$a[] = sprintf(
'<a href="%s" class="tag-link-%d tag-style-%s" style="font-size:%s%s;">%s</a>',
esc_url( $tag->link ),
$tag->term_id,
max( 1, round( ( $tag->count - $count_min ) / $count_unit ) ),
$args['smallest'] + ( ( $tag->count - $count_min ) / $font_unit ),
$args['unit'],
esc_html( $tag->name )
);
}
return implode( $args['separator'], $a );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment