I am currently trying to add styles to the links in the Tag Cloud Widget which I can see uses wp_tag_cloud()
to output the cloud itself. The args in wp_tag_cloud() don’t include a classname which is what I want as styling the the title attribute isn’t very efficient and doesn’t allow for much growth. For example:
.widget .tagcloud a[title~="1"]{
color: red;
}
.widget .tagcloud a[title~="2"]{
color: yellow;
}
.widget .tagcloud a[title~="9999"]{
color: purple;
}
I found the wp_generate_tag_cloud_data
filter which looks to be what I want but I must be missing something as it isn’t having any effect. Here’s what I have so far:
add_filter( 'wp_generate_tag_cloud_data', 'my_tag_cloud_data', 10, 1 );
function my_tag_cloud_data($tags_data){
foreach ( $tags as $key => $tag ) {
$tag_id = isset( $tag->id ) ? $tag->id : $key;
$tag_class="tag-link-" . $tag_id;
$count = $counts[ $key ];
$real_count = $real_counts[ $key ];
if ($real_count > 20){
$tag_class="tag-link-" . $tag_id . ' x-large';
} elseif ($real_count > 15){
$tag_class="tag-link-" . $tag_id . ' large';
} elseif ($real_count > 7){
$tag_class="tag-link-" . $tag_id . ' medium';
} elseif ($real_count > 1){
$tag_class="tag-link-" . $tag_id . ' small';
} else {
$tag_class="tag-link-" . $tag_id . 'x-small ';
}
if ( $translate_nooped_plural ) {
$title = sprintf( translate_nooped_plural( $translate_nooped_plural, $real_count ), number_format_i18n( $real_count ) );
} else {
$title = call_user_func( $args['topic_count_text_callback'], $real_count, $tag, $args );
}
$tags_data[] = array(
'id' => $tag_id,
'url' => '#' != $tag->link ? $tag->link : '#',
'name' => $tag->name,
'title' => $title,
'slug' => $tag->slug,
'real_count' => $real_count,
'class' => $tag_class,
'font_size' => $args['smallest'] + ( $count - $min_count ) * $font_step,
);
}
return $tags_data;
}
I’ve tried playing with the priority and wrapping it in a function tied to the init hook but to no avail. I would be very grateful if someone could tell me what I’m doing wrong here.