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, so I can guarantee that it works. 🙂 $freetag is supposed to be an initialized freetag object.

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

WordPress is a TOTAL NIGHTMARE for posting code.

3 thoughts on “By Request: Tag Clouds with freetag::silly_list

  1. Hi i allways get this error:

    Fatal error: Call to a member function silly_list() on a non-object in D:\apachefriends\xampp\htdocs\fun\test\tag.php on line 56

  2. I have whipped up a GeSHi based inline code-syntax-highlighting plugin for wordpress so that we could have nice looking code for our – it might help you and your “TOTAL NIGHTMARE” situation?! :~) (Sleep is precious).

    It’s available (single php file) with some docs on our wiki – it would be great to see it used by others if you are interested.

    Thanks for your cool freetag work – awesome.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.