-
Notifications
You must be signed in to change notification settings - Fork 35
Melody Cookbook: Listing Your Archives
There are many ways to list archives and many archives that can be listed in Melody. The following samples are samples that show how to list common archive types, but Melody supports a wealth of archive types such as Category Monthly, Author Daily, etc. so don't feel boxed in by this list. Mixing and matching archive types is a specialty of Melody's.
<mt:ArchiveList type="Author">
<mt:If name="__first__">
<ul>
</mt:If>
<li><a href="<$mt:ArchiveLink$>"><$mt:ArchiveTitle$></a></li>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:ArchiveList>
<mt:ArchiveList type="Monthly">
<mt:If name="__first__">
<ul>
</mt:If>
<li><a href="<$mt:ArchiveLink$>"><$mt:ArchiveTitle$></a></li>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:ArchiveList>
The difference between approaches 1 and 2 is that #1 is a raw, alphabetical, non-hierarchal listitng.
<mt:TopLevelCategories>
<mt:SubCatIsFirst>
<ul>
</mt:SubCatIsFirst>
<mt:If tag="CategoryCount">
<li>
<a href="<$mt:CategoryArchiveLink$>"><$mt:CategoryLabel$></a>
<mt:Else>
<li><$mt:CategoryLabel$>
</mt:If>
<$mt:SubCatsRecurse$>
</li>
<mt:SubCatIsLast>
</ul>
</mt:SubCatIsLast>
</mt:TopLevelCategories>
<mt:ArchiveList type="Monthly">
<mt:If name="__first__">
<ul>
</mt:If>
<li><a href="<$mt:ArchiveLink$>"><$mt:ArchiveTitle$></a></li>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:ArchiveList>
This is not available by default. To enable it, navigate through Melody and find the listing archive template (commonly labeled as "Entry Listing") for your site under Theme Dashboard. Click on the link and open the template editor. At the bottom of the screen, you will find a section that says template options. Do the following:
- Click on the Template Options area to expand it.
- Click on the "Create Archive Mapping" link.
- Select "Yearly."
- Click the "Add" buton.
- Click "Save." Note: Yearly archives can take a very long time to publish because they will often have hundreds or even thousands of entries they must process. Two ways to get around this are the following:
- Select "publish queue" for the "publish mode" when you add the archive mapping.
- Implement entry pagination with a limit of 20 to 30 entries per page. Choosing this route may require theme modifications depending on how the theme you are using was implemented. However, most major Melody themes support pagination of entry listings as a standard feature.
Example:
<mt:ArchiveList type="Yearly">
<mt:If name="__first__">
<ul>
</mt:If>
<li><a href="<$mt:ArchiveLink$>"><$mt:ArchiveTitle$></a></li>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:ArchiveList>
<$mt:var name="archives[0]" value="Author"$>
<$mt:var name="archives[1]" value="Category"$>
<$mt:var name="archives[2]" value="Monthly"$>
<mt:Loop name="archives">
<mt:ArchiveList type="$__value__">
<mt:If name="__first__">
<ul>
</mt:If>
<li><a href="<$mt:ArchiveLink$>"><$mt:ArchiveTitle$></a></li>
<mt:If name="__last__">
</ul>
</mt:If>
</mt:ArchiveList>
</mt:Loop>
This one is more advanced, so let's break it down to see what it's doing.
<$mt:var name="archives[0]" value="Author"$>
<$mt:var name="archives[1]" value="Category"$>
<$mt:var name="archives[2]" value="Monthly"$>
This part creates an array called archives. MTML allows you to define an array using the syntax shown above. The first line declares the array, sets its initial size and assigns the first element a value all in one. The second implicitly resizes the array and adds a second value.
The second part is the loop:
<mt:Loop name="archives">
...
</mt:Loop>
Loops take a variable name as their parameter. PHP and Perl developers should think of mt:Loop/ as a foreach loop. This tag creates a local variable named __value__ which always points to the value of the current iteration of the loop (Author in the first, Category in the second, etc. from the example above).
So what's with "$__value__?" That's one of the powerful features of MTML. MTML doesn't require you to do something ugly like this to tell mt:ArchiveList/ what the current value in the loop is:
<mt:ArchiveList type="<$mt:var name="__value__"$>">
...
</mt:ArchiveList>
Obviously, type="$__value__" is cleaner and more readable. It probably also doesn't hurt that using mt:var/ as used in the pseudo-example is not supported by MTML (because it is ugly, overly complex and inelegant).
Those new to Melody will be happy to know that the setvar attribute is universally available to Melody template tags. It lets you convert the output of a tag into a variable, and that variable can then be passed to another tag like in the sample above.