1

I have events that are presented by month. Right now they are being separated by Month but the heading for each chunk shows January as the month for each one. Not sure why, or how to fix it, am interested in understanding both. I am somewhat new to craft.

 <div class="container">

{% if entry.heading is defined %}<h1>{{ entry.heading }}</h1>{% endif %}

{% set allEntries = craft.entries.section('tours').find() %}
{% set allEntriesByMonth = allEntries|group('entryDate.month') %}

{% for month, entriesInMonth in allEntriesByMonth %}
    <h2>{{entry.entryDate|date('M')}}</h2>
        {% for entry in entriesInMonth %}
            <div class="row" style="padding-bottom: 25px;">
                <div class="col-md-12">

                    <div style="border-bottom: 1px solid #d4d4d4;width:100%;">
                        <p>{{entry.entryDate.month}}.{{entry.entryDate.day}}.{{entry.entryDate.year}}&nbsp;&nbsp;-&nbsp;&nbsp;{{entry.locationCity}}, {{entry.locationState}}&nbsp;&nbsp;&nbsp;<a href="/tours/{{entry.slug}}" class="btn btn-primary btn-md" style="float:right;">register</a></p>
                    </div>
                </div>
            </div>
        {% endfor %}        
{% endfor%}

I am trying to adjust the h2 with |date('M') in order to display the date as text. If I do not do that it displays the correct month number, but when I try to format it it breaks and displays the same month for all entries even though it is still ordering them. So how can I format this to show month as text without breaking it?

Any attempt to format the date breaks it. The dates below should be september and october.

enter image description here

  • it is the name of the field that the event happens on, it is set when creating the entry – Rockwell Rice Sep 3 '15 at 20:52
  • How is the output from entry.entryDate formatted? Maybe the output isn't what the date filter expects? – megatrond Sep 3 '15 at 20:53
4

I would suggest to already format entryDate within the group filter in the way you actually want to output it later. To do that use Twig's date filter (→ see all possible formatting options).

{% set allEntries = craft.entries.section('tours').find() %}
{% set allEntriesByMonth = allEntries|group('entryDate|date("F")') %}

{% for month, entriesInMonth in allEntriesByMonth %}
    <h2>{{ month }}</h2>

    {% for entry in entriesInMonth %}
        <div>{{ entry.entryDate|date('m.d.Y') }}</div>
    {% endfor %}
{% endfor%}
2

Sounds to me like you need to use the Twig format filter, I have helped another user out with it here.

So if you want to output the month as a name (January, February etc), you can use this:

<h2>{{ entry.entryDate|format('F') }}</h2>
  • no that breaks it and nothing shows. Which is why I am struggling with this since yesterday now. – Rockwell Rice Sep 4 '15 at 16:31
  • Probably a typo but it should be {{entry.entryDate.format('F')}}... note the dot instead of the pipe to call format(). If you use the pipe, you're calling Twig's format filter (which is like printf). If you use .format() you're calling Craft's internal method which functions like Twig's date(). – RitterKnight Sep 6 '15 at 21:55
  • So every time (including your code above) I try to format that code inside the h2 it breaks. Dot or pipe, both break it and nothing shows. – Rockwell Rice Sep 8 '15 at 13:25
2

You are using entry for two different things. <h2>{{entry.entryDate|date('M')}}</h2> is the entry for the page, and will always be the same. <p>{{entry.entryDate.month}}... is the current entry in entriesInMonth, and will be different each time through the loop.

Here is your current code with some of the html removed for simplicity, and some of the variable names changed for clarity:

{% set allTours = craft.entries.section('tours').find() %}
{% set allToursByMonth = allTours|group('entryDate.month') %}

{% for month, toursInMonth in allToursByMonth %}
    {# this entry will be the same every time around the loop #}
    <h2>{{entry.entryDate|date('M')}}</h2>
    {% for tour in toursInMonth %}
        <p>{{tour.entryDate.month}}.{{tour.entryDate.day}}.{{tour.entryDate.year}}&nbsp;&nbsp;-&nbsp;&nbsp;{{tour.locationCity}}, {{tour.locationState}}&nbsp;&nbsp;&nbsp;<a href="/tours/{{tour.slug}}" class="btn btn-primary btn-md" style="float:right;">register</a></p>
    {% endfor %}        
{% endfor%}

Probably what you want to display in that <h2> is the current month from the outer for loop:

<h2>{{month|date('M')}}</h2>
  • You are correct with what I am trying to do, however with your code (or any variations of it that I have tried) it still shows same month for each section. I added a picture so you can see what I am getting. – Rockwell Rice Sep 4 '15 at 20:35
  • @RockwellRice your key in {% for month, toursInMonth in allToursByMonth %} is already formatted. Change <h2>{{entry.entryDate|date('M')}}</h2> to <h2>{{ month )}}</h2> and you should be good. – carlcs Sep 4 '15 at 21:03
  • All that does is output the number, I need to format it to display the text. So, back to the original question of how do I format that to display the date in text. – Rockwell Rice Sep 4 '15 at 22:05
  • That broke the grouping and only displayed the date as a number. – Rockwell Rice Sep 6 '15 at 19:12
1

Unsure if you still need this, but I found a workaround for your issue. I was also having the same issue when formatting it either broke the output using a filter on the formatted month or it just showed 'January'.

My code was initially similar to yours and I used the '|date('F')' filter on the initial entry postdate to show the month name.

Try and change this part of your output:

{{entry.entryDate.month}}

to

{{entry.entryDate|date('F')}}

Hopefully this works for you too.

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.