Feature
Post

Category
Code


Related Entries in Movable Type

If you’re a good Web 2.0 person, you’ve probably been relentlessly tagging all your entries. You might even have a tag cloud so big it threatens to rain folksonomies. But shouldn’t there be something more useful you can do with your tags? How about linking to related entries? Here’s how we can do it in Movable Type, and without installing any extra plugins.

First, create a new Template Module named “Related” and type this code into the box:

<mt:entryiftagged>
<mt:setvarblock name="curentry"><mt:entryid /></mt:setvarblock>
<mt:setvarblock name="sgtags"><mt:entrytags glue=" OR "><mt:tagname></mt:entrytags></mt:setvarblock>
<mt:setvarblock name="listitems"><mt:entries tags="$sgtags"><mt:setvarblock name="listentry"><mt:entryid /></mt:setvarblock><mt:unless name="listentry" eq="$curentry"><li><a href="<mt:entrypermalink />"><mt:entrytitle /></a></li></mt:unless></mt:entries></mt:setvarblock>
<mt:if name="listitems">
<h3>Related Entries</h3>
<ul>
<mt:var name="listitems">
</ul>
</mt:if>
</mt:entryiftagged>

It’s very important that when you type this in there are no line breaks within any of the <mt:setvarblock></mt:setvarblock> containers. In theory, you should be able to use the strip_linefeeds attribute and not worry about line breaks, but I’ve not had any luck with that. In fact, it’s revealed a very strange bug. Rather than deal with bugs, we’ll just remove the line breaks ourselves.

Now save it and go to your Entry Archive template. Somewhere in there (if you’re using the default templates, right after <$MTInclude module="Entry Detail"$> would be a good choice) add this line:

<$MTInclude module="Related"$>

Republish your blog and you should see a list of related entries on each individual entry page. Let’s take a closer look at our code and see what’s happening.

<mt:entryiftagged>

We don’t want to do any of this if the entry doesn’t have tags.

<mt:setvarblock name="curentry"><mt:entryid /></mt:setvarblock>

We store the ID of the current entry to use later.

<mt:setvarblock name="relatedtags"><mt:entrytags glue=" OR "><mt:tagname></mt:entrytags></mt:setvarblock>

Here, we’re using the <mt:entrytags> container to output a list of tags from the current entry. The glue attribute specifies what text should be between multiple tags. So, if our entry is tagged with cats, pet food, and dogs, we’ll get this:

cats OR pet food OR dogs

And this will be stored in a variable we’ll use on the next line.

<mt:setvarblock name="listitems"><mt:entries tags="$relatedtags"><mt:setvarblock name="listentry"><mt:entryid /></mt:setvarblock><mt:unless name="listentry" eq="$curentry"><li><a href="<mt:entrypermalink />"><mt:entrytitle /></a></li></mt:unless></mt:entries></mt:setvarblock>

We’re using a <mt:entries> container with the tags filter. This will limit the entries returned to only those that have at least one of the tags from the current entry. As we process each entry, we store its ID in a variable. We then use the <mt:unless> conditional tag to compare the ID to the one we stored earlier. This allows us to filter out the current entry from the list we’re creating. If it’s different from the current entry we create a link to that entry and store it in a variable to use in the final output (which is only output if there is anything in that variable):

<mt:if name="listitems">
<h3>Related Entries</h3>
<ul>
<mt:var name="listitems">
</ul>
</mt:if>
</mt:entryiftagged>

Now, this is not necessarily the best way to do related entries. There’s no “scoring” involved to determine which entries are the most related, we just assume they’re related if they have at least one tag in common. And if you’re publishing static files rather than publishing dynamically then old entries won’t have links to new entries unless you republish your entire blog. Still, it’s an easy way to link your entries, and it’s a good example of the complex things you can do with Movable Type variables.


  1. By Pablo posted on February 10, 2009 at 12:44 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Might help to mention which version of MType this is compatible with!

  2. By ali posted on July 12, 2009 at 9:58 am
    Want an avatar? Get a gravatar! • You can link to this comment

    thnx bro , very very useful , from malaysia

  3. By sooran posted on September 16, 2009 at 5:24 am
    Want an avatar? Get a gravatar! • You can link to this comment

    tancks
    this post very very helped me !

    Good Time

  4. Trackback