Home > Freetag, Tech, eatlunch.at > By Request: Tag Clouds with freetag::silly_list

By Request: Tag Clouds with freetag::silly_list

December 6th, 2005

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:

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[] = '<span class="' . $span_class . 
    '" style="font-size: '. $size . $font_units .
    '"><a href="'.$tag_page_url . $tag . '">' . 
    htmlspecialchars(stripslashes($tag)) . '</a></span>';
 
        }       
        $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.

Freetag, Tech, eatlunch.at

  1. shpanky
    December 16th, 2005 at 22:39 | #1

    possible division by zero if $maxqty=$minqty

  2. chrispie
    January 29th, 2006 at 09:01 | #2

    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

  3. Clinton
    May 10th, 2006 at 07:58 | #3

    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. http://www.byteclub.net/wiki/WordPresscshGeSHi_Plugin

    Thanks for your cool freetag work - awesome.

  1. No trackbacks yet.