1

From the Creating an Archive Page for Entries article in the Craft docs. How could I limit it so that it grouped by month but limited things to 6 months?:

{% set allEntries = craft.entries.section('blog').limit(null) %}
{% for date, entries in allEntries | group("postDate|date('F Y')") %}
    <h2>{{ date }}</h2>
    <ul>
        {% for entry in entries %}
            <li>{{ entry.getLink() }}</li>
        {% endfor %}
    </ul>
{% endfor %}
1

100% untested, but something like this should work using Twig's date_modify filter.

{% set startDate = now|date_modify("-6 months") %}

{% set allEntries = craft.entries.section('blog').limit(null).postDate('>= ' ~ startDate) %}

{% for date, entries in allEntries | group("postDate|date('F Y')") %}
    <h2>{{ date }}</h2>
    <ul>
        {% for entry in entries %}
            <li>{{ entry.getLink() }}</li>
        {% endfor %}
    </ul>
{% endfor %}
  • Instead of .postDate() could this also be performed on a custom date/time field (in this case called startDate). I'm having trouble getting it to work: .startDate('>= ' ~ range) – Ian Aug 4 '16 at 10:14
  • Scratch that, I've managed to get it working now. Thanks Brad! :) – Ian Aug 4 '16 at 11:06
  • No worries! @Ian – Brad Bell Aug 4 '16 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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