You are here: Home » Programming » Web Tricks » Creating a Category Entry in MovableType 4

Creating a Category Entry in MovableType 4

I recently posted an entry on making static hierarchies in MovableType. I came up with a much better way of doing this, so I am replacing that entry with this one.

First off, let's create a relationship between a category and the entry which is going to represent it. Create a new category "About Me" and save it. Now create a new entry and call it ".About Me" - the period in UNIX syntax for hidden files, and we are going to hijack it here. :) Attach the entry to category "About Me." You can set this entry to allow comments and trackbacks - it's up to you. You may not like the look of comments at the bottom of a long page of blog entries.

Now we get to alter the templates! First, open Archive Template "Entry Listing" and add the following code to the top of the code:

<MTSetVar name="cat_desc" value="">
<MTSetVar name="cat_extra" value="">
<MTSetVar name="use_short_list" value="0">
<MTSetVar name="use_no_list" value="0">
<MTIfArchiveType archive_type="Category">
 <MTEntries>
  <MTSetVarBlock name="keywords"><$MTEntryKeywords$></MTSetVarBlock>
  <MTIf name="keywords" like="_shortlist"><MTSetVar name="use_short_list" value="1"></MTIf>
  <MTIf name="keywords" like="_nolist"><MTSetVar name="use_no_list" value="1"></MTIf>
  <MTSetVarBlock name="title"><$MTEntryTitle$></MTSetVarBlock>
  <MTSetVarBlock name="cat_title">.<$MTArchiveCategory$></MTSetVarBlock>
   <MTIf name="title" eq="$cat_title">
    <MTSetVarBlock name="cat_desc"><$MTInclude module="Entry Detail"$></MTSetVarBlock>
    <MTSetVarBlock name="cat_extra">
        <$MTInclude module="TrackBacks"$><$MTInclude module="Comments"$>
    </MTSetVarBlock>
   </MTIf>
 </MTEntries>
</MTIfArchiveType>

This establishes our basic variable values if we are dealing with a category entry. Then scroll down and look for the section that prints out the entries. It should look something like this:

<MTIf name="datebased_archive">
    <MTEntries>
<$MTEntryTrackbackData$>
<$MTInclude module="Entry Summary"$>    
    </MTEntries>
<MTElse>
        <MTEntries limit="auto">
<$MTEntryTrackbackData$>
<$MTInclude module="Entry Summary"$>    
        </MTEntries>
</MTIf>

We are going to edit the second bit inside the <MTElse>. Basically take everything from <MTEntries> to </MTEntries> and replace it with this:

 <MTVar name="cat_desc">
 <MTUnless name="use_no_list">
  <!--print out entries inside category:-->
  <MTIf name="use_short_list"><ul class="cat-short-list">
  <MTElse><hr /></MTIf>
  <MTEntries limit="auto">
   <MTSetVarBlock name="title"><$MTEntryTitle$></MTSetVarBlock>
   <MTUnless name="title" like="^\.">
    <MTIf name="use_short_list">
     <li><a href="<$MTEntryPermalink$>"><$MTEntryTitle$></a></li>
    <MTElse>
     <$MTEntryTrackbackData$>
     <$MTInclude module="Entry Summary"$>
    </MTIf>   
   </MTUnless>  
  </MTEntries>
  <MTIf name="use_short_list"></ul></MTIf>
 </MTUnless>

And then tack on the following just below the last </MTIf> from <MTIf name="datebased_archive">:

<MTVar name="cat_extra"><!--:comments and trackbacks if selected-->

You can save the "Entry Listing" template now and republish your site. If you go to category "About Me" now, you should see the entry you created for ".About Me" at the top, followed by a horizontal separator and any other entries you may have listed in that category, blog-style. There are a few things that may still be bothering you. First, your page says "Recently in About Me Category." Just go back to "Entry Listing" and look for where that is printed in the template and replace "Recently in <em><$MTArchiveTitle$></em> Category" with just "<$MTArchiveTitle$>" - that should fix that. Second, you may not like all your entries listed blog-style up against your pretty category entry. Since this is entirely a matter of preference, I set this up as a per-category option. If you add "_shortlist" to the keywords field in the ".About Me" entry, then it will print out the entries as a short list of linked titles instead. If you add "_nolist" to the keywords, it will not print the entries out all (and you can add them to the entry itself in any order or format you like).

You could also easily alter the "Entry Listing" template to always do one of these options - just set the default value to "1" in the first <MTSetVar> at the top of the template code.

So now we have an entry that is tied one-to-one with a category. There's one problem still. Movable Type published the entry in addition to the entry-category. If you click "View Published Entry" from ".About Me," you can see that there is a duplicate page there, which we don't want. This is also the landing page after a user leaves a comment (if you have set up ".About Me" to allow comments). So we are going to edit another template to fix this. Open Archive Template "Entry" now and put this code at the very top of that template:

<MTSetVar name="redirect" value="0">
<MTSetVarBlock name="title"><$MTEntryTitle$></MTSetVarBlock>
<MTIf name="title" like="^\.">
  <MTSetVarBlock name="redirect"><$MTBlogURL$></MTSetVarBlock>
</MTIf>
<MTIf name="redirect">
 <MTEntryCategories>
  <MTSetVarBlock name="cat_title">.<$MTArchiveCategory$></MTSetVarBlock>
  <MTIf name="title" eq="$cat_title">
   <MTSetVarBlock name="redirect"><$MTCategoryArchiveLink$></MTSetVarBlock>
 </MTIf>
 </MTEntryCategories>
</MTIf>
<MTIf name="redirect">
<HTML><HEAD><TITLE><$MTBlogName$></TITLE>
<META HTTP-EQUIV=REFRESH CONTENT="0;URL=<MTVar name="redirect">">
</HEAD>
<BODY><script type="text/javascript">
  location.replace("<MTVar name="redirect">");
</script>
</BODY></HTML>
<MTElse>

You'll want to throw in a final </MTIf> at the very bottom on the template as well. Save this change and republish.

Now when you go to the url for ".About Me," it automatically redirects you back to the category-entry "About Me."

There is one caveat to my solution here: all the category-entries are listed in archives. I wanted it that way, because I view them as entries in their own right (why I set mine up to allow comments). You may not feel the same way. Unfortunately, due to the way most archives are published, I could not find a way to set this on a per-category level. If you want to block/hide all category-entries from archives, edit "Entry Listing" to look like this:

<MTIf name="datebased_archive">
 <MTEntries>
   <MTSetVarBlock name="title"><$MTEntryTitle$></MTSetVarBlock>
   <MTUnless name="title" like="^\.">
     <$MTEntryTrackbackData$>
     <$MTInclude module="Entry Summary"$>    
   </MTUnless>
 </MTEntries>

And edit the Individual Template "Archives" as well. Search for "<li" wherever it mentions "<$MTArchiveTitle$>" and wrap it up like this:

   <MTSetVarBlock name="title"><$MTArchiveTitle $></MTSetVarBlock>
   <MTUnless name="title" like="^\.">
            <li class="archive-list-item"><$MTArchiveDate format="%Y.%m.%d"$>: 
               <a href="<$MTArchiveLink$>"><$MTArchiveTitle$></a></li>
   </MTUnless>

You won't need to change every archive option - I think only Individual actually lists each entry. Per-month archives are handled in "Entry listing" instead.

Anyway, I hope this helps someone. It was a lot of fun to create and I am much happier with MovableType now. Version 4 is really pretty damn good!

Resources: MovableType 4 Templating, MovableType Template Tag Reference

Leave a comment


Type the characters you see in the picture above.