June 28, 2005

Yearly Archives

mtbadge-small.gif

Movable Type is set up for doing date-based archives by day, week, and month, but not by year. There are a few plugins and methods out there that will help you create a yearly calendar of your archives, for example, Brad Choate's Year Archives in MT Perl script plugin will produce a calendar archive (example). Lummox JR's ArchiveYear plugin will produce a similar calendar (example).

I prefer a yearly archive to show a list of entries by month, as I've set up on one of my blogs (example). An archive page like this can be accomplished using the ArchiveYear plugin with some simple changes to the sample code given. Update August 1, 2005 The ArchiveYear plugin seems to not be in its regular location. Arvind has posted a copy of the plugin here. I assume that you would save the text file as archiveyear.pl and load it to the Plugins folder in your MT installation.

yearly-archive-page.gif

1. Download and install the ArchiveYear plugin. (Arvind has posted a copy of the plugin here.)

2. Create a new archive template

In your templates edit window, create a new archive template named "Yearly Archives". Copy and paste the code from your existing date-based template into this new template. If you are using the MT default template, remove all the code between the <div class="content"> tag and its closing div tag.

Add two blocks of code to replace the section you just removed. The first block will be the navigation row that toggles between the years. I have mine set up in a simple table:

<table border="0" cellpadding="0" cellspacing="2" width="200">
<tr>
<td width="80" align="center"><MTArchiveYearPrevious><a href="<$MTArchiveDate format="/path/to/your/archives/%Y/"$>">
&laquo; <$MTArchiveDate format="%Y"$></a></MTArchiveYearPrevious></td>

<td width="80" align="center"><b><$MTArchiveDate format="%Y"$></b></td>

<td width="80" align="center"><MTArchiveYearNext><a href="<$MTArchiveDate format="/path/to/your/archives/%Y/"$>"> <$MTArchiveDate format="%Y"$> &raquo;</a>
</MTArchiveYearNext></td>

</tr>
</table>
<br />

Be sure to replace "/path/to/your/archives/%Y/" with the path to your archives. For example, "/weblog/archives/%Y/"

The next bit of code lays out the months and links to the entries:

<h3><$MTArchiveDate format="%Y"$></h3>

<MTArchiveYear skip="yes">
<MTArchiveYearIfNotBlank>
<MTArchiveYearIfEntries>


<MTArchiveDateHeader>
<p><h2><MTArchiveDate format="%B, %Y"></h2></p>
</MTArchiveDateHeader>

<MTCalendar month="this">

<MTCalendarIfEntries>
<MTEntries>
<a href="<$MTEntryPermalink$>" title="<MTEntryTitle remove_html="1">" >
<MTEntryTitle></a><br /></MTEntries>
</MTCalendarIfEntries>

</MTCalendar>
</MTArchiveYearIfEntries>
</MTArchiveYearIfNotBlank>
</MTArchiveYear>
<br />

The ArchiveYear plugin documentation and the MT manual detail what the various tags do.

Save your template. But do not yet rebuild.

3. Finish creating the template.

For MT 3.2, in your settings window, select publishing. Click on the "Create New Archive Mapping" link. Add a new monthly archive, selecting the Yearly Archives template. Leave the existing monthly template as the preferred monthly template. Create a "custom" archive path for your Yearly template, for example: <$MTArchiveDate format="%Y/index.php"$> .

For MT 3.1 and earier versions, in your weblog config window, select archive. Use the Add New button to create a new archive template. Add another kind of monthly archive, selecting the Yearly Archives template. Leave the existing monthly template as the default. Choose a filename for your yearly archive. For example, <$MTArchiveDate format="%Y/index.html"$>. This will produce a yearly archive URL like so: http://www.yourwebsite.com/archives/2005/index.html. If your site is set up for php, use the index.php extension.

Save the settings and rebuild your monthly archives.


4. Set up links to your monthly and yearly archives from your sidebar

month-and-year-listing.gif

If you use monthly archiving, you probably have a section that links to your monthly archives in your sidebar. If you would like to set up this section so that it shows only links to months from the current year, and links to previous year archives, you will need a couple of additional plugins. MT-SetVarBlock is a tag available from both Brad Choate's Simple Template plugin and from Tim Appnel's mt-varz plugin. You will also need MT-Again. Update 12-11-05: I've posted the code to mt-again here as a zip file. Unpack the zip file and upload the again.pl to your web server to the plugins folder as ASCII text.

Arvind of Movalog has detailed the code needed for this in his Yearly Archives tutorial. I have used his code, but removed the entry count (MTArchiveYearCount) from it.

Place the code block on the sidebar section of your index template where you would like a list of months and years to appear.

<ul>
<MTSetVarBlock name="year"><$MTDate format="%Y"$></MTSetVarBlock>
<MTArchiveYear order="descend">
<MTArchiveYearIfEntries>
<li><a href="<$MTArchiveDate format="http://path/to/my/archives/%Y/%m/"$>" title="See all the entries for <$MTArchiveDate format="%B"$> <$MTGetVar name="year"$>">
<$MTArchiveDate format="%B"$>
<$MTGetVar name="year"$><br />
</a></li>
</MTArchiveYearIfEntries>
</MTArchiveYear>
<MTAgain>
<MTArchiveYearPrevious>
<li><a href="http://path/to/my/archives/<$MTArchiveDate format="%Y"$>/" title="See the Entries from my blogging year of <$MTArchiveDate format="%Y"$>">
Year Of <$MTArchiveDate format="%Y"$>
</a></li>
<$MTAgainHere$>
</MTArchiveYearPrevious>
</MTAgain></ul>

Make sure to again, replace the http://path/to/my/archives/ in the code to the actual path to your archives.


An Alternative

kalsey-dates.gif

If instead, you would like your monthly archive listing to be separated by years, without pointing to yearly archives, as described by Adam Kalsey, that can be accomplished with the MT-Archive-Dateheader plugin, maintained by Tim Appnel.


Many thanks to Arvind Satyanarayan of Movalog for his help figuring out the code in this tutorial. I was stumped, and once again, Arvind saved the day.

Links:
Movalog's Yearly Archives tutorial


Has this tutorial been helpful? Please consider linking to Learning Movable Type at http://learningmovabletype.com/ . Thanks!

Trackback

If you would like to send a trackback
please use the following URL: http://learningmovabletype.com/cgi-bin/mt32/mt-tb.cgi/434

» New archive format from Socialblunders
Many thanks to Elise and her site Learning Movable Type for yet another useful tutorial, this time on how to set up a yearly archive display. Now, with a little code and a few plug-ins, I can tame the beast......[read more]

Tracked: July 9, 2005 05:42 PM

Comments

This is awesome, particularly for those of us who publish in the /year/month/entrytitle.ext format, because I always see a number of people hacking the address to see if /year/ exists. Now I just need to implement it!

Excellent tutorial. But the archive template code above gave me a build error. I played with the code a little and this code works. If yours doesn't work, give this code a try. Replace the second part of the archive code above with this.

<h3><$MTArchiveDate format="%Y"$></h3>

<MTArchiveYear skip="yes">
<MTArchiveYearIfNotBlank>
<MTArchiveYearIfEntries>

<MTArchiveDateHeader>
<p><h2><MTArchiveDate format="%B %Y"></h2></p>
</MTArchiveDateHeader>

<MTCalendar month="this">

<MTCalendarIfEntries>
<MTEntries lastn="1">
<a href="<$MTEntryPermalink$>" title="<MTEntryTitle remove_html="1">" >
<MTEntryTitle>
</a><br />
</MTEntries>
</MTCalendarIfEntries>
</MTCalendar>
</MTArchiveYearIfEntries>
</MTArchiveYearIfNotBlank>
</MTArchiveYear>
<br />

Hi Nick,
Thanks. It looks like you just moved the MTCalendar month="this" down below the MTArchiveDateHeader section. I don't understand all these tags well enough to know why this makes a difference and why the code I gave gives you a build error when it works for me. It took hours of experimentation just to get to work what I did get to work. But thank you so much for posting. Hopefully that will make a difference to anyone encountering problems.

I'm not sure why, but I had to use Nick's modification too. It seems to work though. I guess i won't really know if it works until Jan 2006, as I just switched to MT this month.

Elise, your site has been such a wonderful blessing to me. Thank you so much! :-)

Thank you Elise for sharing the fruits of your "hours of experimentation". I too have spent a lot of time attempting to do this, unfortunately I finished up with nothing to share but frustration.

Thanks Nick for polishing it off. I too got a build error (fixed by moving MTArchiveDateHeader out of MTCalendar):

An error occurred:
Building date-based archive 'Monthly20050601000000' failed: Build error in template 'Yearly Archives': Error in tag: Error in tag: Error in tag: You used an MTArchiveDate tag without a date context set up.

Well, since Nick's modification seems to have been working for so many people, I made adjustments to the tutorial to reflect it. Thanks Nick!

Is there a 2.661 version of this code as well? I tried to find the right place to put the code, but it didn't seem to be there.

Any guidance would be most appreciated.

Is there a way to include the entry count in the yearly archive? I added back in the code but it's showing up a 0 count.

Good article, moreover the alternatives. However, there is not way to get MT-again. It would be great if the plugin (MT-again) could be available as the same as ArchiveYear.

Elise?

Your version of again.pl seems to have a little whitespace corruption in it. Some strings within the script meant to be on separate lines aren't ended with a newline whitespace character.

This causes perl on Unix systems, my system for example, to barf. You may want to check the file whitespace characters carefully and upload a clean copy to this site.

But thanks very much for putting a copy here! I was looking everywhere for a copy of this plug-in!

again.pl does not load correctly in MT 3.2.

The plugin manager disables the plugin.

Any thoughts on why this happens?

Thanks,
Brian

Hi Brian,

Could be the white space issue noted in the comment before yours.

I've uploaded a zip file containing the again.pl that should work and changed the link in the tutorial above.

I'm having problem with getting MT 3.2 to respond to the tags created by the MTAgain and ArchiveYear plugins.

I installed them to my plugins directory as stated and my MT control panel recognizes that the plugins are installed and active.

The monthly archiving listing works perfectly as described above, but when I click on one of the year archives I get an error message screen with the following message:

Smarty error: [in mt:27 line 79]: syntax error: unrecognized tag 'MTArchiveYearPrevious' (Smarty_Compiler.class.php, line 580)

When I remove the offending code and refresh the page it goes to the next new plugin code and gives me the same message. It's like MT is ignoring the tag definitions in the plugins.

Has anyone else had this problem? Or does anyone know how to correct this problem? Thanks.


Post a comment

(Before posting a comment please see the Comments and Trackbacks Policy. Do you need help troubleshooting your weblog? Please post questions and requests for support at the MT Support Forums. Thanks!)




Remember Me?

(you may use HTML tags for style)

Email to a friend

Email this article to:


Your email address:


Message (optional):