Category Archives: eatlunch.at

By Request: Tag Clouds with freetag::silly_list

By request, here’s a quick function I whipped up to generate some basic tag cloud HTML from the output of the silly_list function:

[php] function get_tag_cloud_html($freetag, $num_tags = 100, $min_font_size = 10, $max_font_size = 20, $font_units = 'px', $span_class = 'cloud_tag', $tag_page_url = '/tag/') { $tag_list = $freetag->silly_list($num_tags); // Get the maximum qty of tagged objects in the set $max_qty = max(array_values($tag_list)); // Get the min qty of tagged objects in the set $min_qty = min(array_values($tag_list)); // For every additional tagged object from min to max, we add // $step to the font size. $step = ($max_font_size - $min_font_size)/($max_qty - $min_qty); // Since the original tag_list is alphabetically ordered, // we can now create the tag cloud by just putting a span // on each element, multiplying the diff between min and qty // by $step. $cloud_html = ''; $cloud_spans = array(); foreach ($tag_list as $tag => $qty) { $size = $min_font_size + ($qty - $min_qty) * $step; $cloud_span[] = '' . htmlspecialchars(stripslashes($tag)) . ''; } $cloud_html = join("\n ", $cloud_span); return $cloud_html; }

Feel free to tweak as you please. I wrote the code and tested by adding a tag cloud to eatlunch.at, so I can guarantee that it works. :) $freetag is supposed to be an initialized freetag object.

As a side note, eatlunch.at is horribly unmaintained right now. I’ve got to rewrite it soon.

WordPress is a TOTAL NIGHTMARE for posting code.