Movable Type Categories As Tag Cloud

Like yesterday’s post about del.icio.us tag clouds, this was an experiment because I was bored. I figured it was so easy to make the link tag cloud, it should be almost as easy to create one from my categories. The challenge is that I have almost 1600 entries (actually, this is #1600!): so many that I don’t want the number of entries in a category to correspond directly to the text size (because 200em is really big).
All it took was a little PHP on top of the tags to do the category list.
Of course, this will only work if you can use PHP on your server (and why can’t you, huh huh huh), and if you publish the page as a PHP file. You may want to change the way it increments numbers to get the right scale between categories. Play, and if you end up using it, please post a comment so I can come check it out.
Here’s what I did (of course, this is the “meat” – you have to add the rest of the page around it):


<ul class="tags">
<MTCategories show_empty="0" sort_by="title" sort_order="ascend">
<?
$count = <MTCategoryCount>;
$fontSize = 1;
for ( $i=1;$i<10; $i++ ) {
if ( $count > $i ) {
$fontSize = $fontSize + .1;
}
}
for ( $i = 10; $i < 300; $i=$i+20 ) {
if ( $count > $i ) {
$fontSize++;
}
}
?>
<li>
<a href="<MTCategoryArchiveLink>" title="<MTCategoryCount> Entries" style="font-size:<?= $fontSize ?>em"><MTCategoryLabel></a>
</li>
</MTCategories>
</ul>

The CSS is pretty much the same as yesterday, although I’ve tweaked it just a little since then:

ul.tags {
list-style:none;
text-align:justify;
}
ul.tags li {
display:inline;
padding:0 .3em;
}

5 thoughts on “Movable Type Categories As Tag Cloud”

  1. Thanks for the tip! I used a different php script before and was having trouble on the category archive pages (it was only showing the categories associated with the entries listed on the category, instead of all of them). Your script did the trick and allowed me to show a consistent cloud on all pages (including category archives).

  2. Hey thanks for the nifty work. I’ve been tweaking it – and got the scaling you did to work for the del.icio.us tags themselves, since my tags were ending up too huge.
    The code for that didn’t work as it was posted, I had to as a end tag somewhere, and strip the ul and li, for some reason on my page….
    Thanks!!

  3. a) You rock!
    b) I was able to modify your script to create a category tag cloud without category archiving. check it out in action here: http://www.mig81.com/index.php?page=zeitgeist
    The code is here: http://www.mig81.com/temp/zeitgeist.phps
    I know this might seem like a work-around, but some people might be able to use this sort of solution.
    c) Is there a way to adjust the increment at which the text size is adjusted? Some of the text is too small…
    d) You rock! Keep up the good work!

  4. P.S. You’ve got an error on your comment submission page: Odd number of elements in hash assignment at lib/MT/App/Comments.pm line 71. Use of uninitialized value in list assignment at lib/MT/App/Comments.pm line 71. Odd number of elements in hash assignment at lib/MT/App/Comments.pm line 71. Use of uninitialized value in list assignment at lib/MT/App/Comments.pm line 71.
    Secondly, about the script: I know how to adjust the increment, but is there a way to set minimum font size, for example, so it is not completely unreadable?

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>