Weekly Archives in ExpressionEngine

 

I recently had a client site come through with a design that featured news items on the home page.  Nothing unusual there.  But the design also had a large drop-down that allowed users to select the week they wanted to see news for.

The functionality I needed was exactly the same as the native Monthly Archive tagset, but for weeks rather than months.

If you’e used the Monthly Archive tagset, you know that it creates links that take advantage of the dynamic aspect of the exp:channel:entries tag in that the Monthly Archive tag generates URLs that have month and year as URL segments (example.com/blog/2011/06/) and that URL structure is how an exp:channel:entries tag running in dynamic mode knows which entries to pull.

Pretty cool, but for me it means that the means by which EE can pull monthly archives is pretty well-baked into the system and not easily tweaked for my purposes.  Searching the EE aftermarket reveals a Yearly Archives plugin by Low, but that’s making the bucket bigger rather than smaller.  Not finding any other add-ons I had actually written and sent an email to the client saying I couldn’t do the weekly drop-downs and had setup the page to use a monthly drop-down and the native Monthly Archives function and that would have to do until/unless I “had a bright idea”.

And - you guessed it - it wasn’t 1/2 hour after sending that email when I had that bright idea.  Or at least enough of a spark to get me doing the experimentation that led to a solution. Below is the solution I came up with - it’s working on our development system with some test content.  It’s not a perfect solution - let’s look at the code and then I’ll point out my concerns with it (note that this is a simplified version not involving a drop-down):

<h3>Choose Week</h3>
{!-- This creates a list of weeks that contain news entries.  The links it builds are to the same template as this code appears on
The week number is appended onto the URL 
for use in the next loop --}
{exp
:channel:entries dynamic="no" channel="news" orderby="date" sort="desc" limit="52" disable="member_data|pagination|categories|custom_fields" }
    {date_heading display
="weekly"}<a href="{path='news/date'}/{week_date format="%W"}">Week {week_date format="%F %d %Y"}</a><br />{/date_heading}
{
/exp:channel:entries}

<hr />

{!-- This loop lists the entries for the selected week --}
{exp
:channel:entries dynamic="no" channel="news" disable="member_data|pagination|categories|custom_fields" orderby="date" sort="asc"}
    {
!-- Compare week_date of entry to number appearing in url and show if a match --}
    {if {week_date format
="%W"== segment_3}
        {title} 
{entry_date format="%F %d %Y"}<br />
    
{/if}
{
/exp:channel:entries} 

So, two loops.  Let’s look at the first one:

Date Heading
ExpressionEngine has a blogging heritage (although that’s less obvious as time goes on).  One of the parameters for the workhorse Channel Entries tag pair is the date_heading tag that allows you to group channel entries in certain time-buckets (daily, weekly, monthly, etc).  The functionality it’s bringing to the table here is letting me build a list of weeks that have entries. 

Week Date
I’m using the week_date variable here - I think for the first time ever in an EE project.  The week_date variable establishes the starting point for the week-bucket to group our entries into.  It defaults to starting weeks on Sundays, if you want to change that use the start_day parameter.

Link with Custom Segment
The final point of this first loop is to get the unique week number into the URL.  I’m doing that by taking advantage of EE’s segment variables.  To create the link I’m just appending on a custom segment to the normal link built by the path variable.

The first loop nets me a list of linked weeks that have entries in them.  Clicking one of the links refreshes the page but with a week number now appearing in the URL - now let’s look at goes on in the second loop.

This is pretty straightforward - we set dynamic to off so this tag ignores the URL.  Then it loops through all entries in the news channel and compares the week of the entry date to the week number appearing in the URL.  If it’s a match the title is shown.

Performance?
My only concern with this approach is performance.  In my case I don’t expect the news channel to have thousands of entries but rather dozens with maybe 8-10 entries per week. This particular project is also not a public one, but rather with a couple of dozen targeted and logged in users.  So for me I think the performance will be OK.  If your project requirements are different this approach may need some stress testing before using.

Not Quite Done
The above code works great after a week has been chosen, but I also needed the page to display the current week’s news by default.  Here’s my revised code:

<h3>Choose Week</h3>
{!-- This creates a list of weeks that contain news entries.  The links it builds are to the same template as this code appears on
The week number is appended onto the URL 
for use in the next loop --}
{exp
:channel:entries dynamic="no" channel="news" orderby="date" sort="desc" limit="52" disable="member_data|pagination|categories|custom_fields" }
    {date_heading display
="weekly"}<a href="{path='news/date'}/{week_date format="%W"}">Week {week_date format="%F %d %Y"}</a><br />{/date_heading}
{
/exp:channel:entries}

<hr />

{!-- This portion of the code builds the resulting list news --}
{if segment_3 
==""} {!-- No week number is in the URLso this is the default view before choosing a different week --}
    {exp
:channel:entries dynamic="no" channel="news" display_by="week" limit="1" disable="categories|member_data|pagination|custom_fields"}
        {title} 
{entry_date format="%F %d %Y"}<br />
    
{/exp:channel:entries}
{
/if}
            
{if segment_3 
!=""} {!-- Specific week has been chosen so week number is appearing in URL --}
    {exp
:channel:entries dynamic="no" channel="news" disable="member_data|pagination|categories|custom_fields" orderby="date" sort="asc"}
        {
!-- Compare week_date of entry to number appearing in url and show if a match --}
        {if {week_date format
="%W"== segment_3}
            {title} 
{entry_date format="%F %d %Y"}<br />
        
{/if}
    {
/exp:channel:entries}
{
/if} 

The last loop is the same, just wrapped in the conditional that looks for a week having been selected.  The newly-added middle loop runs if no date has been selected and the key there is the display_by parameter that returns a single week’s worth of entries.  I may also have to use the show_current_week parameter but I’m not sure yet and am waiting for the system to be used more to see if the right results are returned.

So there’s my “bright idea” for weekly archives in ExpressionEngine.  Now if I’d just had it before telling the client it couldn’t be done…

 

Category Navigation

<< Previous Entry   

Next Entry >>

 

Add Your Comment

Commenting is not available in this channel entry.

Unless otherwise stated all content is © Michael Boyink of Train-ee.com & Boyink Interactive. Please don't steal - I've got kids to feed...

dy>