Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
1  
Superb, and I didn't even have to answer. ;) –  Steve Abraham Jul 7 at 14:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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