Weighted Category Lists

After a few site tweaks earlier in the week allowed me to implement new categories in this weblog, I decided to add weighted category lists, much like Flickr & Technorati.

I modified an example given by hitormiss, which looks like this:

<?php
$minfont = 1;
$maxfont = 5;
$fontunit = "";
<MTCategories>
$catlinks{"<$MTCategoryLabel$>"} = "<$MTCategoryArchiveLink$>";
$counts{"<$MTCategoryLabel$>"} = <$MTCategoryCount$>;
</MTCategories>

$spread = max($counts) – min($counts); if ($spread <= 0) { $spread = 1; };
$fontspread = $maxfont – $minfont;

$fontstep = $spread / $fontspread; if ($fontstep <= 0) { $fontstep = 1; };
foreach ($counts as $catname => $count)
{
$catlink = $catlinks{$catname};
print "<a href=\"$catlink\" title=\"$count entries\" class=\"cat".
ROUND(($minfont + ($count/$fontstep)))."\">$catname</a> &nbsp;\n";
}
?>

[You can also download it in a text file]

Put it in your Moveable Type archive template, and you’re good to go. It will write 5 CSS classes — cat1, cat2, cat3, cat4, cat5… (of course, you’ll have to define those in your stylesheet!)

You can head over to my archives page, to check out the implementation. (Notice that the 5 largest categories are the original 5… i will be classifying old entries in the next couple of days…)

(I apologize if not all the code is viewable here — I suggest you highlight and copy/paste to a text editor, which should work)

0 Responses to “Weighted Category Lists”


Comments are currently closed.