I'm trying to create an archive list template that lists entries from a News or Blog Channel based on year/month/day combinations.

However, I get no entries and can't see where I'm going wrong...I'm running the latest build of Craft.

{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}
{% set month = craft.request.segment(4) %}
{% set day = craft.request.segment(5) %}

Year: {{ year }}<br>
Month: {{ month }}<br>
Day: {{ day }}<br><br>

{% if (year | length) and (month | length) and (day | length) %}
    {% set entryDate = year ~ '-' ~ month ~ '-' ~ day %}
{% elseif (year | length) and (month | length) %}
    {% set entryDate = year ~ '-' ~ month %}
{% elseif (year | length) %}
    {% set entryDate = year %}
{% endif %}

Entry Date: {{ entryDate }}<br><br>

{% set entries = craft.entries.find({section: "news", postDate: entryDate}) %}

{% if entries | length %}
    Entries found.
{% else %}
    No entries found.
{% endif %}
  • 2
    Just asked you on g+: Where does the code fail? Does it work if you set the entryDate manually? – carlcs Jun 12 '14 at 15:33

I've used this method for sorting by year and month (it could be expanded to support days).

On the news/index.html template:

{% if month is defined %} {# show an archive of a month #}

    {% set sortDate = year ~ '-' ~ month %}

    Entry Date: {{ sortDate }}

    {% if month != 12 %}
        {% set nextmonth = month+1 %}
        {% set nextyear = year %}
    {% else %}
        {% set nextmonth = 01 %}
        {% set nextyear = year+1 %}
    {% endif %}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year ~ '-' ~ month).before(nextyear ~ '-' ~ nextmonth) %}

{% elseif year is defined %} {# show an archive of a year #}

    Entry Date: {{ year }}

    {% set entriesToShow = craft.entries.section('news').limit(null).after(year).before(year+1) %}

{% else %} {# assume overview page #}

    {% set entriesToShow = craft.entries.section('news') %}

{% endif %}

Any entries can then be displayed by one loop.

{% for entry in entriesToShow %}

Since this is made to be in on the section's index template you don't need a separate archive template. Just setup a few routes in the CP to send the archive tokens back to the index template.

Set all the token combinations you need like:

news/archive/(month)/(year)

To route to:

news/index.html

I ended up getting my archive list working using 'after' and 'before' parameters like below.

{% if year is not defined %}
    {% redirect "news/archives/"~now.year %}
{% endif %}

{% set year  = craft.request.segment(3)  %}

{% set month = craft.request.segment(4) %}

{% set day = craft.request.segment(5) %}

Year: {{ year }}<br>
Month: {{ month }}<br>
Day: {{ day }}<br><br>

{% if (year | length) and (month | length) and (day | length) %}

    {% set entryAfterDate = year ~ '-' ~ month ~ '-' ~ day %}

    {% set entryBeforeDate = year ~ '-' ~ month ~ '-' ~ (day + 1) %}

{% elseif (year | length) and (month | length) %}

    {% set entryAfterDate = year ~ '-' ~ month %}

    {% set entryBeforeDate = year ~ '-' ~ (month + 1) %}

{% elseif (year | length) %}

    {% set entryAfterDate = year %}

    {% set entryBeforeDate = (year + 1) %}

{% endif %}

After Entry Date: {{ entryAfterDate}}<br>
Before Entry Date: {{ entryBeforeDate}}<br><br>

{% set entries = craft.entries.find({section: "news", order: "postDate", status: "Live", after: entryAfterDate, before: entryBeforeDate}) %}

{% if entries | length %}

    Entries found.

{% else %}

    No entries found.

{% endif %}

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.