2

I'm trying to create an archive of PDF newsletters in Craft 3. I want to create a list of entries sorted by month and year. Then each year would appear separately. Finally, I want each asset within the entry to be linked to the month name for download.

I can get each entry to output by month - but it is not separating by year. I'm also not certain how to call just the asset in this loop. Would it be better to link to an entry and then show the asset there?

Here's what I have so far:

{# all entries sorted by year/month   #}
{% set allEntries = craft.entries.section('sectionName').limit(null).orderBy('postDate desc') %}


  {% for year, entriesInYear in allEntries.all() | group("postDate.year") %}
      <div class="w-col w-col-medium-3 w-col-3" data-mh="years">
        <h3>{{ year }}</h3>

        {% for month, entries in entriesInYear | group("postDate|date('F')" ) %}

            {% for entry in entries %}
                {% for asset in entry.file %}<a href="{{ asset.getUrl }}" class="w-inline-block">
                {% endfor %}
            {% endfor %} 

                {{ month }}</a> 

        {% endfor %}

      </div>
  {% endfor %}

This outputs:

    <div class="w-col w-col-medium-3 w-col-3" data-mh="years">
        <h3></h3>

<a href="/media/newsletter-06-2018.pdf" class="w-inline-block">             <a href="/media/newsletter-06-2017.pdf" class="w-inline-block">             <a href="/media/newsletter-06-2016.pdf" class="w-inline-block"> 

June</a> 

<a href="/media/newsletter-05-2018.pdf" class="w-inline-block">             <a href="/media/newsletter-05-2017.pdf" class="w-inline-block">             <a href="/media/newsletter-05-2016.pdf" class="w-inline-block"> 

May</a> 

</div>
  • can you edit with input/output? – Clark Nelson Jun 7 '18 at 17:49
  • ok updated with output. Not seeing year and months are all grouped together instead of in separate years. – glider Jun 8 '18 at 2:48

Your Answer

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

Browse other questions tagged or ask your own question.