<p>Instead of using pagination you might want to look into setting up a <a href="http://buildwithcraft.com/docs/routing#dynamic-routes" rel="nofollow noreferrer">dynamic route</a> in the format "calendar/[year]/[month]". Adding 'year' and 'month' tags to your route will essentially pass those values as named variables to your template, which you can use to retrieve and display entries, as well as generate navigation to next/previous month's events. If you want the uri '/calendar' (without the 'year/month' uri segments) to link to the current months events you can test for the existence of the 'year' and 'month' variables, and if not defined, set the start month to the first day of the current month.</p>

<pre><code>{% if year is defined and month is defined %}
    {% set startMonth = year ~ '-' ~ month ~ '-' ~ 1 %}
{% else %}
    {% set startMonth = now|date_modify("first day of this month") %}
{% endif %}
{% set nextMonth = startMonth|date_modify('+1 month')) %}
{% set previousMonth = startMonth|date_modify('-1 month')) %}

{% set entries = craft.entries.section('calendar').postDate('and', '&gt;= ' ~ startMonth, '&lt; ' ~ nextMonth %}

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

&lt;div class="pagination"&gt;
    &lt;a href="{{ 'events/' ~ nextMonth|date("Y/n") }}"&gt;next month&lt;/a&gt;
    &lt;a href="{{ 'events/' ~ previousMonth|date("Y/n") }} "&gt;previous month&lt;/a&gt;
&lt;/div&gt;
</code></pre>

<p>If required, within any month you could also use a pagination tag if you needed to limit the number of events displayed on any one page to 15.</p>

<p>For additional examples see these answers, <a href="https://craftcms.stackexchange.com/a/5456/784">here</a> and <a href="https://craftcms.stackexchange.com/a/9527/784">here</a>.</p>

CC BY-SA 3.0
2015-06-26T07:30:12.120Z
Thanks for the example. I've never used Routes before. How would my actual route look like when the calendar in question is an include that is part of my homepage?

Would it be "calendar/year/month" ? Which template should I specify it to load then? Since it's an include, and not a template in the way I think of a template?
Filadelfia
CC BY-SA 3.0
2015-06-26T08:02:54.150Z
Also, curious if it's possible to actually display the month name when paginating backward/forward? So instead of saying "Previous Month", it would say "May".

BTW: your current code works perfectly, except for the previous/next links, because of the Route part not being correct set up.
Filadelfia
CC BY-SA 3.0
2015-06-27T16:42:43.803Z
Sorry, I think the month number without the leading zero is represented by 'n' instead of 'm'. I'll update the answer. See the [php date docs](http://php.net/manual/en/function.date.php) for more info. To output the month name, I believe you can use 'F': `<a href="{{ 'events/' ~ nextMonth|date("Y/n") }}">{{ nextMonth|date("F") }}</a>`
Douglas McDonald
CC BY-SA 3.0
2015-06-27T16:57:58.733Z
Regarding the include question — if your looking to include events on the homepage, you might want to consider using ajax to load the calendar (to keep the uri's clean). Or perhaps consider having the "hompage" calendar (which shows the current month's events) link to the full calendar for previous/future events. Otherwise, if you want to incorporate routes with the homepage you would probably want to drop the 'calendar' segment of the route and just use `[year]/[month]` as your route, and specify the homepage template as the route template. The calendar could still be a template include.
Douglas McDonald
CC BY-SA 3.0
2015-06-25T16:05:40.230Z
2015-06-27T16:43:04.140Z
2017-04-13T12:51:00.253Z
Douglas McDonald
<p>I'm working on creating a simple calendar based on only native Craft fields. While coding this, I'm having a hard time getting the pagination to work properly.</p>

<p>The calendar currently lists all upcoming events this current month. Next/Previous should then paginate the calendar 1 full month forward/backward (not 1 month backwards from todays date, but all entries in the next/previous month).</p>

<p>According to Brandon, paginate.prevUrl/nextUrl won't map to specific date-based starting points. So, how do I tackle this problem then?</p>

<p>Here's my current code:</p>

<pre><code>&lt;div class="row"&gt;
    &lt;div class="small-12 medium-12 large-12 columns"&gt;
        &lt;div class="widget_event single_widget box_shadow"&gt;
            &lt;h3&gt;Hva skjer i Filadelfia&lt;/h3&gt;

            {% set startDateTime = now|date_modify('0:00')|date('c') %}
            {% set endDateTime = now|date_modify('first day of next month 4:00')|date('c') %}

            {% set startDateParam = 'and, &gt;= ' ~ startDateTime ~ ', &lt; ' ~ endDateTime %}

            {% set events = craft.entries.section('calendar').limit('15').startDate(startDateParam).order('startDate asc') %}

{% for event in events %}
    &lt;div class="event_list"&gt;
        &lt;div class="single_event"&gt;

            &lt;div class="date_left"&gt;
                &lt;p class="date"&gt;{{ event.startDate | date("d") }}.&lt;/p&gt;
                &lt;p class="day"&gt;{{ event.startDate | date("M") }}&lt;/p&gt;
            &lt;/div&gt;

            &lt;div class="title_middle"&gt;
                &lt;p class="title"&gt;{{ event.title }}&lt;/p&gt;
                &lt;p class="desc"&gt;{{ event.body | hacksaw(words='75', allow='&lt;b&gt;&lt;strong&gt;&lt;i&gt;&lt;em&gt;') }}&lt;/p&gt;
            &lt;/div&gt;

            &lt;div class="time"&gt;{{ event.startDate | date("H:i") }}&lt;/div&gt;

        &lt;/div&gt;
    &lt;/div&gt;
{% endfor %}


{% paginate craft.entries.section('calendar').limit(15) as pageEvents %}
    {% for month, entries in pageEvents | group("postDate | date('F')") %}
        &lt;div class="event_nav text-center"&gt;
            &lt;span class="left left_arrow"&gt;{% if paginate.prevUrl %}&lt;a href="{{ paginate.prevUrl }}"&gt;Previous Month&lt;/a&gt;{% endif %}&lt;/span&gt;
            &lt;span class="right right_arrow"&gt;{% if paginate.nextUrl %}&lt;a href="{{ paginate.nextUrl }}"&gt;Next Month&lt;/a&gt;{% endif %}&lt;/span&gt;
        &lt;/div&gt;
    {% endfor %}
{% endpaginate %}

        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<pagination>
Paginate based on date-based starting points
# Paginate based on date-based starting points

(Asked by: username_0 on 2015-06-25)

I'm working on creating a simple calendar based on only native Craft fields. While coding this, I'm having a hard time getting the pagination to work properly.
The calendar currently lists all upcoming events this current month. Next/Previous should then paginate the calendar 1 full month forward/backward (not 1 month backwards from todays date, but all entries in the next/previous month).
According to Brandon, paginate.prevUrl/nextUrl won't map to specific date-based starting points. So, how do I tackle this problem then?
Here's my current code:

    
        
            Hva skjer i username_0

            {% set startDateTime = now|date_modify('0:00')|date('c') %}
            {% set endDateTime = now|date_modify('first day of next month 4:00')|date('c') %}

            {% set startDateParam = 'and, >= ' ~ startDateTime ~ ', < ' ~ endDateTime %}

            {% set events = craft.entries.section('calendar').limit('15').startDate(startDateParam).order('startDate asc') %}

{% for event in events %}
    
        

            
                {{ event.startDate | date("d") }}.
                {{ event.startDate | date("M") }}
            

            
                {{ event.title }}
                {{ event.body | hacksaw(words='75', allow='') }}
            

            {{ event.startDate | date("H:i") }}

        
    
{% endfor %}


{% paginate craft.entries.section('calendar').limit(15) as pageEvents %}
    {% for month, entries in pageEvents | group("postDate | date('F')") %}
        
            {% if paginate.prevUrl %}Previous Month{% endif %}
            {% if paginate.nextUrl %}Next Month{% endif %}
        
    {% endfor %}
{% endpaginate %}

**Tags:** pagination


---

## 1 answers

### Answer by username_1 on 2015-06-25. Score: 1 | Accepted answer

Instead of using pagination you might want to look into setting up a dynamic route in the format "calendar/[year]/[month]". Adding 'year' and 'month' tags to your route will essentially pass those values as named variables to your template, which you can use to retrieve and display entries, as well as generate navigation to next/previous month's events. If you want the uri '/calendar' (without the 'year/month' uri segments) to link to the current months events you can test for the existence of the 'year' and 'month' variables, and if not defined, set the start month to the first day of the current month.
{% if year is defined and month is defined %}
    {% set startMonth = year ~ '-' ~ month ~ '-' ~ 1 %}
{% else %}
    {% set startMonth = now|date_modify("first day of this month") %}
{% endif %}
{% set nextMonth = startMonth|date_modify('+1 month')) %}
{% set previousMonth = startMonth|date_modify('-1 month')) %}

{% set entries = craft.entries.section('calendar').postDate('and', '>= ' ~ startMonth, '< ' ~ nextMonth %}

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


    next month
    previous month


If required, within any month you could also use a pagination tag if you needed to limit the number of events displayed on any one page to 15.
For additional examples see these answers, here and here.

> **Comments:**
> **username_0**: Thanks for the example. I've never used Routes before. How would my actual route look like when the calendar in question is an include that is part of my homepage?  Would it be "calendar/year/month" ? Which template should I specify it to load then? Since it's an include, and not a template in the way I think of a template?  
> **username_0**: Also, curious if it's possible to actually display the month name when paginating backward/forward? So instead of saying "Previous Month", it would say "May".  BTW: your current code works perfectly, except for the previous/next links, because of the Route part not being correct set up.  
> **username_1**: Sorry, I think the month number without the leading zero is represented by 'n' instead of 'm'. I'll update the answer. See the [php date docs](http://php.net/manual/en/function.date.php) for more info. To output the month name, I believe you can use 'F': `<a href="{{ 'events/' ~ nextMonth|date("Y/n") }}">{{ nextMonth|date("F") }}</a>`  
> **username_1**: Regarding the include question — if your looking to include events on the homepage, you might want to consider using ajax to load the calendar (to keep the uri's clean). Or perhaps consider having the "hompage" calendar (which shows the current month's events) link to the full calendar for previous/future events. Otherwise, if you want to incorporate routes with the homepage you would probably want to drop the 'calendar' segment of the route and just use `[year]/[month]` as your route, and specify the homepage template as the route template. The calendar could still be a template include.  

---

