Building a Weighted Category List


Published on December 27, 2004 at 2:05 PM EST
In the Tutorials category.

A while ago I read What’s this blog about?, which showed a weighted list of keywords that appears on his site. I thought that was a cool idea, to show—based on font size—the most written-about topics.

What I put together isn’t anywhere near that complex. The weighted category list simply assigns a font size to the category name based on the number of entries associated with that category. Here’s a snapshot of danandsherree.com’s weighted category list:

About Dan (Old Stuff) Boy Scouts Christmas Computers Crafts danandsherree.com News Elsewhere in our Life Girl Scouts Movable Type Music, Movies and Television Photography Pinball the Cat Random News Bits RC Cars Reviews Travelogues and Photos Work-Related

Below is the code I used to build that list. You’ll need to be able to use PHP to use this, and it’ll have to go into an Index Template to work.

<p>
<MTCategories show_empty="1"><?php

// Set the number of entries required to jump a font size.
$a = 5;
$b = 10;
$c = 15;
$d = 20;
$e = 25;
$f = 30;
$g = 40;

if (<MTCategoryCount> <= $a ) {
 echo '<span style="font-size: 75%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $b ) {
 echo '<span style="font-size: 100%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $c ) {
 echo '<span style="font-size: 125%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $d ) {
 echo '<span style="font-size: 150%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $e ) {
 echo '<span style="font-size: 175%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $f ) {
 echo '<span style="font-size: 200%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

} elseif (<MTCategoryCount> <= $g ) {
 echo '<span style="font-size: 250%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';

// Really big category!
} else {
   echo '<span style="font-size: 300%;"><a href="<MTCategoryArchiveLink>" title="<MTCategoryDescription remove_html="1" encode_php="q">"><$MTCategoryLabel$></a></span> ';
} ?>

</MTCategories>
</p>

This is obviously some very simple code. The variables $a through $g are compared against the number of entries in a category. You’ll probably want to change them to reflect your site. Specifically, $a should be a little bigger than your smallest cateogry and $g should probably be a bit bigger than your largest. So, for a larger site something like this might be better:

$a = 15;
$b = 30;
$c = 45;
$d = 60;
$e = 75;
$f = 90;
$g = 105;

It’s probably also worth pointing out that there’s an “overflow” in the above, for if your site grew and you didn’t remember to change the variable (“//Really big category!”).

I think I picked some ok font sizes, but it’s easy enough to change them—look at the style="font-size: <Percent>%; pieces. Just change the Percent number to something you like.