« Alternating Comment Styles with Movable Type 4 | Main | Upgrading to Movable Type 4 with MTCommentFields »

Tag Cloud on PHP in Movable Type 4

Movable Type has become one of the first platforms in which tags have appeared . It happened in the third version. And in the fourth version it is possible to add tags not only to post, but also to uploaded files or created pages. But functionality of the tag MTTags is not thought over up to the end in the current version as well as in the previous version MT. It is necessary to make some efforts to have a normal Tag Cloud.

In the third version it was possible to deduce the list of all tags, excepting an opportunity of restriction of their numbers. The cloud was expected to turn out. When tags became more, they turned into a cloud and occupied the most part of the screen.

In the fourth version an opportunity has been added to establish the numbers of  displayed tags, and also their sorting by ranks. Now on the site it's possible to show a cloud  which will include 50 of the most popular tags. The numbers of tags in a cloud is set individually, but such opportunity pleases. But there’s an ill luck: when sorting by ranks tags are displayed in this way — the more popular the tag is, the higher it will be, or on the contrary. A simple example:

Tag Cloud on Movable-Type.ru

It does not suit at all as the visual perception of such cloud does not cause any desire to click under the link. Therefore it is necessary to resort to PHP’s help.

Create Normal Tags Cloud

  1. First of all you must change extension of files on .php. For this just go to the settings of the blog, then to the tab “Publishing”. Enter “php” near by the item “File Extension for Archive Files” (without a point in the beginning). After that all archives (posts, categories, pages, etc) will be published with new extension. But some index files, for example, Main Index and Archives, will stay with old extensions. Therefore you will have to go to the section Templates and manually specify the necessary extension for these templates.
  2. Then it is necessary to create an index tempalate (we call it TagCloud, with a name of the published  file tagcloud.php) which will be automatically published with the following contents:

    <?
      $tagslist = array(<MTTags limit="50" sort_by="rank" glue=", ">"<$MTTagName$>" => <$MTTagRank max="10"$></MTTags>);
      ksort($tagslist);
      reset($tagslist);
      while (list($tag, $tagsize) = each($tagslist))
      {
       echo '<a href="http://example.com/tag/'.rawurlencode($tag).'" class="t'.$tagsize.'" rel="tag">'.$tag.'</a>';
       echo "\n";
       }
    ?>
    Download the example with the comments.
  3. If you want template TagCloud to be displayed on other pages of the site you should include it in these pages. It is made with the help of function include in PHP:

    <? include ("/home/user/example.com/template-path/tagcloud.php");  ?>

    If  you have standard templates this line is necessary to be set in a Sidebar template (“Sidebar (3-column)” or “Sidebar (2-column)”). Find a code <MTTags limit ="20" sort_by ="rank"> finishing with </MTTags>. Delete it and interchange it with a line from the third point. Pay attention that you must  specify an absolute way to a template. You can learn about if you look in the blog settings.

Well that’s all, now you should have normal Tags Cloud or something like this:

Tag Cloud on ProBlog.ru
Reference Links

  • Everybody loves tags!
    Look at the point “Beautifying the tag search link URLs” and learn how to make beautiful URL for tags.

Thanks Zara Khudaverdyan for the help in translation of tutorial into English language.
This tutorial on Russian language.

Comments (6)

Just a quick note ... the class should actually be:

class=".rank-'.$tagsize.'"

The class that gets built with the example in the tutorial is .t1 to .t10, but the new default templates for MT4 actually use .rank-1 to .rank-10

Also, It's probably best to use <$MTTagSearchLink$> and <$MTBlogSitePath$> when referencing the link for the tag anchors or the site path for the PHP include. One less thing to mess around with if you ever need to change things around.

I have a quick question, that might be simple, but I only found that it's not possible. I'm installing a fresh MT 4.0 install to upgrade on my site, and when I imported my blog entries over to the new MT 4.0 blog, tags weren't included.

Is there anyway to import tags with the entries? I only recently started using them the last few months, but I'd be disappointed to lose them completely.

Hi Jaclyn -

I can only assume you are upgrading from MT, but you don't mention which version. If you are upgrading from MT 3.3x, then you are in luck (sort of). The tags can be easily exported, you just need to make a small change to the export routine to get the data into your export prior to importing. Read the entry I wrote on the subject.

If you are on a prior version, it means you are using the Tags plugin, and that makes it harder. Not impossible, but not as easy. You should be able to adapt the instructions to the method in that case, but you may not have the same results. You could also upgrade to MT 3.35, then to MT4, but it's an extra step.

You could also simply upgrade your database, rather than exporting and re-importing (make sure you have a good backup before trying this method).

Zapp:

Hello, I tried to use your code but I get this error:

Parse error: syntax error, unexpected '{', expecting ')' in /home/domain/folder/cgi-bin/mt/php/extlib/smarty/libs/Smarty.class.php(1936) : eval()'d code on line 33

Do you know why the php is not working?
Thank you

@Zapp -

That sounds like you have a mismatched tag. Look closely and make sure that when you open a brace you close it with a brace, and the same is true with parentheses - {} and (). You'll also need to make sure that they are in the right place as laid out above, because the different symbols mean different things, so they need to be in the right places.

Unfortunately, since PHP is interpreted by the server, it's not going to be possible to troubleshoot remotely (and line 33 is specific to your system). But hopefully this will help!

Post a comment

(If you haven't left a comment here before, your comment may need to be approved before will appear on the entry. Thanks for waiting.)