2

I'm new to Craft from ExpressionEngine and wondered how people handle Month/Year archive pages. I have figured out how to get the archives to display but the code seems overly complex. I wondered if there is a more straightforward approach or if it is possible to display your archive using the same template as the news/index which displays all. In EE this was possible and just wondered if it was in Craft.

Thanks in advance. My archive template code below.

{% if month is not defined %}
    {% set before = year + 1 %}
    {% set after  = year %}
{% else %}
    {% if month == 12 %}
        {% set yearBefore = year + 1 %}
        {% set monthBefore = '01' %}

        {% set before = yearBefore~'-'~monthBefore %}
        {% set after = year~'-'~month %}
    {% else %}
        {% set before = year ~ '-' ~ (month + 1) %}
        {% set after  = year ~ '-' ~ month %}
    {% endif %}
{% endif %}

{% set allEntries = craft.entries.section('news').limit(null).after(after).before(before) %}

{% for date, entries in allEntries | group("postDate|date('F Y')") %}
    <h3>Entries from {{ date }}</h3>

    {% for entry in entries %}
        <article class="post">
            {% for asset in entry.newsthumb %}
                <div class="image-wrapp">
                    <a href="{{ entry.url }}"><img src="{{ asset.url('newsthumb') }}" alt="{{ asset.title }}"> </a>
                </div>
            {% endfor %}

            <div class="text-wrapp">
                <h3><a href="{{ entry.url }}">{{ entry.title }}</a></h3>

                <time datetime="2017-07-27" class="post-time">{{ entry.postDate.format('F d, Y') }} </time>

                {{ entry.newssummary }}

                {% if entry.newscategory | length %}<ul class="category-list">{% endif %}
                {% nav category in entry.newscategory %}
                    <li><a href="{{ category.url }}">{{ category.title }}{{ not loop.last ? ', ' }}</a></li>
                {% endnav %}

                {% if entry.newscategory | length %}</ul>{% endif %}
            </div>
        </article>
    {% endfor %}
{% endfor %}
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
fodney
  • 193
  • 7

1 Answers1

1

The way I do this is to use a different template for each set of entries. So there would be an archive one for all the articles from a given month: _news/index-archive, and a category one for all the articles in a given category: _news/index-category, and one for all of the entries: _news/index-all, maybe one for articles by tag, etc.

Each of these templates sets entries to the relevant entries, and then does

{% include '_news/index-generic' with {
  entries: entries
} only %}

And index-generic has all the shared display code.

Probably there will be a couple of other things that vary as well, for example a heading:

{% include '_news/index-generic' with {
  entries: entries,
  heading: 'Category: '~category.title
} only %}
Marion Newlevant
  • 12,047
  • 22
  • 55