Archive of Related Entries with Movable Type

February 26, 2008 | Code Snippets, Thoughts from the team

Recently, I have been working on a Movable Type based website for monthly research data. We found that we needed to create a sidebar module displaying monthly entries grouped by year. After some head scratching I came up with the following MT4 code.

<MTSetVarBlock name="category"><MTEntryCategory></MTSetVarBlock>

<h3>Historic Data</h3>

<$MTSetVar name="year" value="0"$>

<MTEntries sort_order="descend" category="$category">

<MTIfNotEqual a="[MTEntryDate format='%Y']" b="[MTGetVar name='year']">
  <MTIfNotEqual a="[MTGetVar name='year']" b="0"></ul></MTIfNotEqual>
  <h4><$MTEntryDate format="%Y"$></h4><ul>
  <MTSetVarBlock name="year"><$MTEntryDate format="%Y"$></MTSetVarBlock>

<li><a href="<$MTEntryPermalink$>"><$MTEntryTitle$></a></li>


This code sets a variable, $category, containing the name of the category associated with the current entry. It then selects all the entries in this category in descending date order. A $year variable then tracks the year of the last entry, printing out a heading when the year changes.

More like this