3

I've created a custom route for my entries based on week like so:

'events/(?P<year>\d{4})/(?P<week>\d{2})' => 'events/_weekindex',

I know i can group entries by week like this:

{% set allEntriesByWeek = allEntries|group('postDate|date('Y-W')') %}

What i'm not quite sure how to do is how to show navigation ONLY for weeks that have entries present, and then of course output the entries in my template when those navigation items are clicked.

1 Answer 1

4

For generating the nav, this should work.

{% set allEntriesByWeek = allEntries|group("postDate|date('Y-W')") %}
{% for week, entry in allEntriesByWeek %}
    <li><a href="events/{{ week|replace('-','/') }}">{{ week }}</a></li>
{% endfor %}

For grabbing entries (using the 'week' and 'year' variables passed via the route).

{% set startDate = now.setISODate(year, week) %}
{% set endDate = startDate|date_modify("+7 days") %}
{% set entries = craft.entries.section('events').postDate('and', '>= ' ~ startDate, '< ' ~ endDate ) %}
{% for entry in entries %}
    {{ entry.title }}
{% endfor %}

For pagination.

<a href="events/{{ startDate|date_modify('+7 days')|date('Y/W') }}">Next Week</a>
<a href="events/{{ startDate|date_modify('-7 days')|date('Y/W') }}">Previous Week</a>
2
  • Why is it that your weeks are only 6 days long?
    – carlcs
    Commented May 1, 2015 at 7:35
  • Difference between first and last day of week is 6, and using less than or equal to :) but you are probably right, should be 7 and less than. Commented May 1, 2015 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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