Formatting WordPress Tag Cloud
April 1st, 2008 um 2:00 Uhr • Uncategorized • 1 Kommentar •
While the wp_tag_cloud() function is a very cool addition to the WordPress content manager, the widget is not easily customized for layout and design. For starters, the classes generated are completely unique names (read: they are ids in class clothing) and the tag cloud widget does not employ a unique id. Consequently, the use of CSS to conform the tag cloud ul and li tags seems out of the question. When I converted about 200 categories to tags on my personal blog I got the following mess:

Here’s what the code out of the box looks like for the wp_tag_cloud() function:
function wp_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => ''
);
$args = wp_parse_args( $args, $defaults );
$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
if ( empty($tags) )
return;
$return = wp_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
if ( is_wp_error( $return ) )
return false;
$return = apply_filters( 'wp_tag_cloud', $return, $args );
if ( 'array' == $args['format'] )
return $return;
echo $return;
}
By changing the defaults array I was able to correct my layout problem:
$defaults = array(
'smallest' => .9, 'largest' => 1.8, 'unit' => 'em', 'number' => 75,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => ''
);
“Smallest” and “Largest” refer to the font size that can be given a unit of either pts or em. I changed mine smallest from 8 to .9 and largest from 22 to 1.8. and the unit from pts to em. If you would prefer an unordered list of your tags (as opposed to a cloud), change the “format” key from “flat” to “list“. The “number” key is the number of tags that will be displayed. Default is 45. I have a ton so I increased my number to 75.
The result of these changes was what I was looking for and would hope that functionality might be eventually built in at least for CSS if not some sort of form for changing the values of the default values in the wp_tag_cloud() function itself. Here’s what my tag cloud ended up looking like:
