1

I am creating a website for not-for-profit organization that help victum of domestic violence. I have a page called Event which display all fund raising events under each month.

I have setup Date/Time field and 12 months tabs and want to know how to display the entries for each month. For example I assign two entries for Feb and when I click on Feb tab, the entries will display.

Here is the sample code I wrote and copied Marion Newlevant's suggestion but when I tested it does not work. Any ideas?

Here is what my html structure to display the entries for each month.

<div class="recent-events2">
  <ul class="month-tabs">
    <li{% set startDate = year~'-'~month~'-01' %}</li>
  </ul>

  <div class="events">

    {% set monthEvents = craft.entries
      .section('events')
      .dateField(['and', '>= '~startDate, '< '~endDate])
    %}
    <div class="item">
      <div>

        {% for asset in entry.eventThumbnail %}
          {% if asset in entry.eventThumbnail %}
            <img src="{{ asset.getUrl('eventThumbnail') }}" alt="{{ asset.title }}">
          {% else %}
            <img src="{{ siteUrl }}public/assets/img/grey_image_placeholder.jpg" alt="Skyfoundation">
          {% endif %}              
        {% endfor %} 

        <h2>{{ entry.title }}</h2>
        <span>{{ entry.subtitle }}</span>
        <p>{{ entry.eventBlurp}}</p>

        {% for block in entry.eventOrganiser %}
          {% if block.type == "eventDate" %}
            <div class="ticket-cart"> 
              <a href="{{ block.ticketLink }}" class="ticket-btn">Buy Ticket</a>
              <div>{{ block.ticketLeftover }} / <i>{{ block.ticketAvail }}</i> tickets left</div>
            </div>
          {% endif %}
        {% endfor %}

      </div>
    </div>
  {% endfor %}

  </div>

</div>
  • is year set? is month set? Do startDate and endDate have sensible values? Once you have those values correct, you will want to loop over the monthEvents entries – Marion Newlevant Feb 17 '17 at 3:49
  • Both year and month are set. But with limited of coding skills to how to write the correct code to display the entries for each month. It would be good if you show me how. – William Spark Feb 20 '17 at 0:29
1

Assuming your date/time field is called dateField, and your entries channel is events.

You want to fetch just the entries that have dates in that month. You can do that like this:

If you know the year and the month, the start of the month is (~ is the twig string concatination operator):

{% set startDate = year~'-'~month~'-01' %}

And the start of the next month is:

{% set endDate = startDate|date_modify('+1 month')|date('Y-m-d') %}

We use twig's date_modify filter to get the start of the next month, and then twig's date filter to convert that to a string.

Now you can fetch the entries for the month. You want ones where the dateField is on or after startDate and before endDate.

{% set monthEvents = craft.entries
  .section('events')
  .dateField(['and', '>= '~startDate, '< '~endDate])
%}
| improve this answer | |

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.