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 %}