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 wondering how I would replicate the ExpressionEngine date_heading tag pair in Craft?

{date_heading}  {entry_date format="%Y %m %d"}  {/date_heading}

The date heading can be used to show a heading at certain intervals. The interval can be set to show hourly, daily, weekly, monthly, or yearly.

share|improve this question
1  
Could you clarify the ultimate goal here? Is it to show entries grouped by dates? –  Stuart Whitehead Jun 21 at 14:50
    
Yep... that's exactly what I'd like to do. –  Jesse Bennett-Chamberlain Jun 21 at 15:02
add comment

1 Answer 1

up vote 7 down vote accepted

Group:

Take a look at Craft's custom group filter. Your example could look like this:

{% set allEntries = craft.entries.section('blog').find() %}
{% set allEntriesByDay = allEntries | group('postDate') %}

{% for day, entriesInDay in allEntriesByDay %}
    <h1>{{ day }}</h1>

    {% for entry in entriesInDay %}
        {{ entry.title }}
    {% endfor %}
{% endfor %}

.

Conditionals:

You could also save the last entries date and use conditionals:

{% set allEntries = craft.entries.section('blog') %}
{% set last_date = '' %}

{% for entry in allEntries %}
    {% if last_date < entry.date %}
        This entry is the first of a new day
    {% endif %}

    {% set last_date = entry.date %}

    {{ entry.title }}
{% endfor %}
share|improve this answer
    
This might be a dumb question... but how do I reformat the {{ day }} variable in your first example? If I do something like: {{ day|date("D") }} It changes every instance to the same date. –  Jesse Bennett-Chamberlain Jun 21 at 16:29
1  
@JesseBennett-Chamberlain You're right, my mistake. I accidentally added .day to postDate which means only the numerical value was passed. I edited the answer, it works now. –  Victor In Jun 21 at 16:42
add comment

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.