You only want to add a class name based on the counts. Your above code looks like you may have copy/pasted from somewhere but you don’t need all that.
I just tested this with wp_generate_tag_cloud_data
(#L869) and wp_tag_cloud()
and it’s working.
Unfortunately for a basic test site like mine, the small count
represents the largest number of tags for me. You might want to add your sizes based on a normalized font_size
. Essentially do some math to turn it into 0-1 and select your classes with that value — not count
.
add_filter( 'wp_generate_tag_cloud_data', 'my_tag_cloud_data', 10, 1 );
function my_tag_cloud_data( $tags_data ) {
foreach ( $tags_data as $key => $tag ) {
// get tag count
$count = $tag [ 'real_count' ];
// adjust the class based on the size
if ( $count > 20 ) {
$tags_data [ $key ] [ 'class' ] .= ' tag x-large';
} elseif ( $count > 15 ) {
$tags_data [ $key ] [ 'class' ] .= ' tag large';
} elseif ( $count > 7 ) {
$tags_data [ $key ] [ 'class' ] .= ' tag medium';
} elseif ( $count > 1 ) {
$tags_data [ $key ] [ 'class' ] .= ' tag small';
} else {
$tags_data [ $key ] [ 'class' ] .= ' tag x-small ';
}
}
// return adjusted data
return $tags_data;
}
CSS
<style>
.tag.x-large {
color: red;
}
.tag.large {
color: green;
}
.tag.medium {
color: blue;
}
.tag.small {
color: yellow;
}
.tag.x-small {
color: orange;
}
</style>