x
 
Create New Page
 View Previous Changes    ( Last updated by jejuna )

Yearly Archives

Question:

How can I make an archive index of titles by year?

Plugin

You can do this with the Yearly Archives plugin

Answer by Weblog Entries Tag

{exp:weblog:entries weblog="default_site" disable="member_data|trackbacks|categories|custom_fields" limit="30" paginate="both"}
{date_heading display
="yearly"}
<h1><a href="{path="site/by-year"}{entry_date format="%Y"}">{entry_date format="%Y"}</a></h1>
{/date_heading}
<li><a href="{comment_url_title_auto_path}">{title}</a></li>
{paginate}
<div class="pagination">
<
p>Page {current_page} of {total_pages} pages {pagination_links}</p>
</
div>
{/paginate}
{
/exp:weblog:entries}

Answer by Query:

This can be done using the Query module and URL segments.
Here is a starter; you can embellish it as needed, although you’ll need some understanding of SQL coding to do so.

Note: these examples all assume that they are going to be placed in the “weblog” template group. If you’re putting them into a different template group, be sure to change the {path} variables.
More recent versions of EE use the template group called site.

The Yearly Archive Template

Create a new template for your yearly archive page.
In the example given here, the template is named “year”.
You’ll probably want to copy your regular “archives” template as a starter.
If you don’t have an “archives” template,
you can copy your “index” template.

This template will be accessed through a URL that looks something like:

http://yourdomain.com/index.php/weblog/year/2003

where the year to be displayed is added to the URL right after the template name.
We will use the EE segment variable {segment_3} to get the year to be displayed out of the URL.

Replace the logic part of the template with something like this:

<h4>Archive for the year {segment_3}</h4>

{exp:weblog:entries weblog="{master_weblog_name}" dynamic="off" year="{segment_3}" sort="asc"}

{date_heading display
="monthly"}
<br />
<
h4>{entry_date format="%F %Y"}</h4>
{/date_heading}

<a href="{comment_url_title_auto_path}">{title}</a><br />

{/exp:weblog:entries}

The important part of that code, the part that makes it a yearly archive, is:

dynamic="off" year="{segment_3}"

in the parameter list for exp:weblog:entries.

The entries will be listed in normal date order, with the oldest first.
This is controlled by the “sort=” parameter.
If you want them with the newest first, change “asc” to “desc”.

Performance note: in the example code above, we aren’t using any of the optional parts of the exp:weblog:entries data.
You could add the parameter
disable="categories|custom_fields|member_data|pagination|trackbacks"
to the exp:weblog:entries tag to reduce the amount of unnecessary work that EE needs to do to produce the page.

The List of Years the Simple but slow Way

Place something like the following code in each template that needs to have the list of years, with the links to the yearly archive pages.

{exp:weblog:entries weblog="{master_weblog_name}"
disable="categories|custom_fields|member_data|pagination|trackbacks"
cache="yes" refresh="1440"
dynamic="off" sort="desc"}

{date_heading display
="yearly"}

<a href="{path=weblog/year}{entry_date format="%Y"}">Year {entry_date format="%Y"}</a><br /> {/date_heading}

{
/exp:weblog:entries}

The years are in reverse date order, with the current year first.
This is controlled by the “sort=” parameter.
If you want them with the oldest year first, change “desc” to “asc”.

This technique works and you can easily embellish it without knowing anything about SQL, but it can be terribly inefficient.
It extracts all of the entries from the weblog and then runs through them,
generating a link only when the year changes.
Its performance would be much worse without the “disable=” and tag-caching parameters ("cache=" and “refresh=").

If this list will appear on your most-viewed pages,
you probably should be concerned about the performance.
The tag-caching is crucial for top performance of this technique;
the settings given above will only recalculate the list once per day,
so on January 1st of a year, that year might not appear in the list right away.
You can adjust the refresh time as desired.

Bear in mind, though, that tag-caching is overridden by template caching.
If you use this code in a page that has template caching turned on,
the generated list of years will only be cached for as long as the page is cached.
Whenever the page is regenerated, the list of years will be regenerated.

To give you an idea of what kind of performance concerns there might be:
on one weblog with about 300 entries,
generating the list of years took about 1/20 of a second this way after the tag had been cached.
Without tag-caching, it took about 1/4 of a second.
Without tag-caching and without the “disable=” parameter, it took about 2/3 of a second.
In contrast, the technique described in the next section took about 1/100 of a second.

The List of Years, the Maximum Performance Way

Place something like the following code in each template that needs to have the list of years, with the links to the yearly archive pages.

{exp:query sql="SELECT
DISTINCT YEAR(FROM_UNIXTIME(`entry_date`)) AS `year`
FROM `exp_weblog_titles`
ORDER BY `year` DESC"
}

<a href="{path=weblog/year}{year}">Year {year}</a><br />

{/exp:query}

This query provides one variable:
* year, the year

The years are in reverse date order, with the current year first.
This is controlled by the “ORDER BY” clause above.
For example, if you want them with the oldest year first, change “DESC” to “ASC”.

This listing of years includes all years from all weblogs.
If you only want years that have entries for a particular weblog,
you’ll need to add a WHERE clause to the SELECT statement after the FROM clause.
It would look something like:

WHERE `weblog_id` in (1,2)

The weblogs are identified by their numbers, which you’ll need to find on your “Weblog Management” page in the Control Panel.

Category:Queries Category:Archives

Categories: