2

I'm outputting a timeline for company history and i'd like to add a graphic for the Year before the first entry of each year. My current code is as follows:

{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}

{% for moment in companyHistory.children.type('history') %}
    <li>{{ moment.title }}</li>
{% endfor %}

My date field is simply called 'date'. How might I go about this with the fewest possible DB queries?

3

I got it using the 'group' filter at : http://buildwithcraft.com/docs/templating/filters#group

Thanks to Steve Abraham for pointing me in the right direction. My code that seems to work is:

{% set companyHistory = craft.entries.section('about').slug('company-history').first() %}
{% set allEntriesByYear = companyHistory.children.type('history') | group('date.year') %}

{% for year, entriesInYear in allEntriesByYear %}
    <h2>{{ year }}</h2>
    {% for moment in entriesInYear %}
        <li>{{ moment.title }}</li>
    {% endfor %}
{% endfor %}

Let me know if there is an even better solution.

1
  • 1
    Superb, and I didn't even have to answer. ;) Jul 7 '14 at 14:23

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.