Published by Mijingo

movie icon image

ExpressionEngine How-to Articles

Topics range from beginner to advanced, but are all born out of real world, professional, day-to-day use of ExpressionEngine. Need more information? Get training videos and ebooks on ExpressionEngine from Mijingo.

Yearly Archive with YearList

A few years ago, a client needed to list out all of the years in which there were articles on the site and allow people to click on the years and get a list of all of the articles in that year; the result being an archive of the entries by year. For those of you that have tried, you probably know that ExpressionEngine does not handle this out-of-the-box. But it does help us about half-way.

My solution was very simple. I wanted to create a list of years and make them clickable so I can access a results page with all of the articles from that year.

<ul>
    <
li><a href="/blog/year/2009/">2009</li>
    <
li><a href="/blog/year/2008/">2008</li>
    <
li><a href="/blog/year/2007/">2007</li>
    <
li><a href="/blog/year/2006/">2006</li>
</
ul

In this example, I’m using a template group called blog and a template called year (this is the exact setup I have here at EE Insider). The year at the end of the URL is what we’ll use to call up the needed entries from ExpressionEngine. Of course, the year in the URL and in the list element would be dynamically created.

There were two requirements for my list of years:

  1. It should only include years in which there are articles
  2. It has to be completely automatic; no manual updating of the years or any other maintenance should be required.

My solution was to create a very simple plugin, called YearList, that returned all years in which there were entries in the specified weblog and then I would use those years to leverage existing functionality in ExpressionEngine.

Sure, I could have built a huge, complex plugin that did everything for you except tie your shoes, but by keeping it extremely simple, the plugin is flexible and you can use it as you see fit.

Let’s get started.

Get the YearList Plugin

First, you want to download the YearList plugin from Github and install it in ExpressionEngine by copying the pi.yearlist.php file to the system/plugins directory.

The plugin uses the following tag pair:

{exp:yearlist weblog="yourWeblog" category="1"}
 {year}
{
/exp:yearlist} 

Just like the weblog entries tag pair, you set the weblog you want to use with the weblog parameter. The category parameter is optional, but could be useful in some situations. The {year} variable is the only variable available and it outputs the 4 digit year.

Making the List of Years

For EE Insider, I’ve placed the year list at the bottom of the existing Blog Archive page. It doesn’t matter where you put, as long as it makes sense for the site you’re building.

We want to combine the static unordered list code at the top of this article with the YearList tag pair code just above. Together, they’ll make the list of years we need.

<ul>
  
{exp:yearlist weblog="blog"}
    
<li><a href="/blog/year/{year}">{year}</a></li>
  
{/exp:yearlist}
</ul

Pretty simple stuff. The YearList plugin only returns years in which there are actual entries in the database. This way we don’t get any blank years (e.g. maybe you didn’t publish anything in 2005) and the years are automatically generated.

We’re using the {year} variable to create the URL that sends the user to the year archive page and to also display the year in the unordered list item.

Now we just have to set up the year template so it displays the articles from the year specified in the URL.

Lighting Up the Year Archive Page

We now go to the year template in the blog template group and add in some simple code to make the articles appear for the year set in the last segment of the URL.

This is only the pertinent parts of the template; I’ve left everything else out.

{exp:weblog:entries weblog="blog" year="{segment_3}" dynamic="off"}
  
<h2>{title}</h2>
  <
h3>{entry_date format="%M %d, %Y"}</h3>
  
{blog_body}
{
/exp:weblog:entries} 

We’re using a normal Weblog Entries Tag pair but included the year parameter, which tells EE to only return entries from that year. We grab the third segment of the URL, which contains the year and set that as the value of the year parameter.

Screenshot of the year archive on EE Insider

Year archive listing on EE Insider Blog

To see a version of this technique in action, I’ve added a year archive listing to the EE Insider Blog Archive. For this version I limited the list to 15 entries and enabled pagination. To make pagination and dynamic=“off” work together, I had to make a minor adjustment to ExpressionEngine. See below for a link to how I did it.

More Information

Posted on Aug 17, 2009

Filed Under: How-To, ExpressionEngine Development, Web Development,

Ryan Irelan
About Ryan Irelan

Ryan Irelan is the Technology and Development Director at Happy Cog, a web design and development firm. He is a noted ExpressionEngine expert, having created a wildly popular video training series on ExpressionEngine. Additionally, Ryan is the publisher of EE Insider, a well-known news and information site for the ExpressionEngine community. In his spare time, Ryan is the production director of A List Apart Magazine, which is one of the most popular ExpressionEngine-powered sites on the web. Recently, Ryan published a book on ExpressionEngine 2 called "ExpressionEngine 2: A Quick-Start Guide."