Display Entries in a calendar I have a section for holding calendar entries stored by postDate in the CMS.
On the front end I want to basically display these in a calendar.

I have code which displays the calendar looping through each day of the month.
For each day it currently queries if there is any entries for this day.

Is there a better way to do it? - my current method had a query for each day?

Current code is:

&lt;table class="cal"&gt;
  &lt;thead class="hide-for-small"&gt;
    &lt;tr&gt;
      &lt;th width="300"&gt;Sun&lt;/th&gt;
      &lt;th width="300"&gt;Mon&lt;/th&gt;
      &lt;th width="300"&gt;Tue&lt;/th&gt;
      &lt;th width="300"&gt;Wed&lt;/th&gt;
      &lt;th width="300"&gt;Thu&lt;/th&gt;
      &lt;th width="300"&gt;Fri&lt;/th&gt;
      &lt;th width="300"&gt;Sat&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
    {% set time = "now"|date("U") %}        
    {% set daysInMonth = time|date('t') %}
    {% set startDow = time|date('F 1\\s\\t Y')|date('w') %}
    {% set dow = startDow %}
    {% for day in range(1,daysInMonth) %}
        {% if loop.first and startDow != 0 %}
            &lt;td colspan="{{ startDow }}"&gt;&lt;/td&gt;
        {% endif %}
        &lt;td&gt;
            &lt;div&gt;
                &lt;span class="day"&gt;{{ day }}&lt;/span&gt;
                    {% set today=thisyear ~ "/" ~ thismonth ~ "/" ~ day %}

                    {% if (category is defined) and (category is not empty) %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d'), relatedTo: category } %}
                    {% else %}
                    {% set params = { section: 'calendar', status: null, postDate:today|date('Y-m-d') } %}
                    {% endif %}
                    {% set entries = craft.entries(params)%}
                    {% if entries|length %}
                &lt;div&gt;
                    {% for entry in entries %}
                    &lt;span class="event green"&gt;{{ entry.title }}&lt;/span&gt;
                    {% endfor %}
                &lt;/div&gt;
                    {% endif %}
            &lt;/div&gt;
        &lt;/td&gt;
        {% if loop.last and dow != 6 %}
            &lt;td colspan="{{ 6 - dow }}"&gt;&amp;nbsp;&lt;/td&gt;
        {% endif %}
        {% if dow == 6 %}
            {% set dow = 0 %}
        &lt;/tr&gt;
        &lt;tr&gt;
        {% else %}
            {% set dow = dow + 1 %}
        {% endif %}
    {% endfor %}
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
I haven't tested this, but it should be a good starting point, at the very least.

{# Determine the number of days in the month #}
{% set daysInMonth = "now"|date("t") %}

{# Determine the date range spanning the month #}
{% set firstDayOfMonth = "now"|date("Y-m-01") %}
{% set firstDayOfNextMonth = "now"|date_modify("first day of next month")|date("Y-m-01") %}
{% set postDateParam = "and, &gt;= " ~ firstDayOfMonth ~ ", &lt; " ~ firstDayOfNextMonth %}

{# Create our parameters object #}
{% if (category is defined) and (category is not empty) %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate', relatedTo: category} %}
{% else %}
    {% set params = {section: 'calendar', postDate: postDateParam, status: null, order: 'postDate'} %}
{% endif %}

{# Retrieve all of the events for the month, and group them by day #}
{% set groupedEvents = craft.entries(params)|group('postDate|date("Y-m-d")') %}

{# Set a shortcut for the YYYY-MM portion of the current month #}
{% set yearMonth = "now"|date("Y-m") %}

{# Loop through all of the days in the month #}
{% for day in range(1, daysInMonth) %}
    {% set formattedDay = "%02d"|format(day) %}
    {% set dayOfMonth = yearMonth ~ "-" ~ formattedDay) %}

    {# If the current day is a key in our groupedEvents array, we have events #}
    {% if dayOfMonth in groupedEvents|keys %}
        &lt;span class="day"&gt;{{ dayOfMonth }}&lt;/span&gt;

        &lt;div&gt;
            {% for event in groupedEvents[dayOfMonth] %}
                &lt;span class="event green"&gt;{{ event.title }}&lt;/span&gt;
            {% endfor %}
        &lt;/div&gt;
    {% endif %}
{% endfor %}


As I said, I haven't tested the above code, but the basic principle works (I've used it for grouping people by surname, and so forth).

Let me know if anything is unclear, I realise it's quite the chunk of Twig.

Stephen