<p>The code on the left is what you want to use... Here are the general steps to follow to put everything together.</p>

<p>Since you're creating an archive page, you have to explicitly tell Craft what to do. First step then is to set the URL to something like:</p>

<pre><code>href="/chamber-news/archive/{{ month }}/{{ year }}"
</code></pre>

<p>Then set up a template (my example uses one named "chamber-archive") which looks to the URL for the month &amp; year to determine which entries to load. (<a href="https://craftcms.stackexchange.com/a/2875/103">altered code borrowed from this answer</a>)</p>

<pre><code>{% set month = craft.request.segment(3) %}
{% set year = craft.request.segment(4) %}

{% set firstDay = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDay = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set archive  = craft.entries.section('sectionNameHere').postDate('and', '&gt;= ' ~ firstDay, '&lt;= ' ~ lastDay) %}
</code></pre>

<p>Then you need to set up routing to tell Craft to use this template for this URL pattern.</p>

<p><a href="https://i.sstatic.net/OsoWY.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OsoWY.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2015-09-22T11:57:19.693Z
Sorry, I can't get this to work.  I've followed your instruction but it's just loading the _layout template :(
Martin
CC BY-SA 3.0
2015-09-22T15:00:48.193Z
Small note to this: You'd be able to simply call the `month` and `year` tokens as `{{ month }}` and `{{ year }}`, instead of manually setting them. Craft passes your tokens to route templates as their variable, per [documentation](http://buildwithcraft.com/docs/routing#dynamic-routes).
Taylor Daughtry
CC BY-SA 3.0
2015-09-22T16:09:21.837Z
Very good to know @TaylorDaughtry. Thank you for adding this!
Anna_MediaGirl
CC BY-SA 3.0
2015-09-22T16:13:19.260Z
@martin Show me screenshots of each piece that you've completed please. Does it appear that just the routing part isn't working?
Anna_MediaGirl
CC BY-SA 3.0
2015-09-22T20:12:02.643Z
So I have the following so far... https://i.sstatic.net/bzGsQ.jpg

I'm not sure if it has any bearing but i'm also using this method for directing Structure entries to their own templates.  http://craftcms.stackexchange.com/questions/96/how-can-i-route-a-specific-entry-in-a-structure-section-to-its-own-template

Many thanks

Martin
Martin
CC BY-SA 3.0
2015-09-22T20:23:22.027Z
Try adding "Hello World" to the news-archive.twig template to see if it's loading but just not showing entries... That will help you decide in which direction to troubleshoot. Also, set the URL in chamber-news.twig to start with a slash at the beginning. Additionally, print out {{ month }} and {{ year }} in news-archive.twig to ensure the variables are getting loaded correctly. You may need to add a slash at the start of the route URL as well.
Anna_MediaGirl
CC BY-SA 3.0
2015-09-22T20:24:43.103Z
Also, you can use {{ month | lower }} when creating the URL to make the month name lower case.
Anna_MediaGirl
CC BY-SA 3.0
2015-09-23T08:30:58.397Z
I've tried the "Hello World" thing and it doesn't show.  I've also tried the slash.  In the Docs it says not to use a slash at the beginning of a route.  I know it's only loading the _layout template because the page that does load just contains the _layout info.  Curiously I tried this and it does load the template http://cliveportman.co.uk/code/archives-by-month-using-craft-cms, however it still doesn't show any content!? :(
Martin
CC BY-SA 3.0
2015-09-23T14:43:06.503Z
That example is pretty much the same method so use that code if you have it working.
Anna_MediaGirl
CC BY-SA 3.0
2015-09-25T08:31:46.717Z
Thanks... but although it shows the right template, it doesn't show any content :/
Martin
CC BY-SA 3.0
2015-09-25T16:47:57.843Z
Please post the contents of your template somewhere.
Anna_MediaGirl
CC BY-SA 3.0
2015-09-21T17:51:05.483Z
2015-09-21T18:06:05.387Z
2017-04-13T12:51:00.160Z
Anna_MediaGirl
<p>Why not use <a href="https://buildwithcraft.com/docs/templating/entrymodel#getUrl" rel="nofollow">getUrl</a> instead of getLink? </p>

<pre><code>&lt;li&gt;&lt;a href="{{ entry.getUrl() }}"&gt;{{ month }} ({{ entries | length }})&lt;/a&gt;&lt;/li&gt;
</code></pre>

CC BY-SA 3.0
2015-09-22T14:45:59.980Z
2015-09-22T14:45:59.980Z
Chris
<p>So P&amp;T chipped in to help and of course solved my plight immediately :D</p>

<p>For anyone else who might encounter a similar scenario I'm posting the solution.</p>

<p>On the page containing the archive list:</p>

<pre><code>{% set allEntries = craft.entries.section('sectionName').limit(null) %}

&lt;ul id="archive"&gt;
    {% for year, entriesInYear in allEntries | group("postDate.year") %}

        {% set counter = ( counter | default(0) ) + 1 %}

        &lt;li class="year{{ counter }}"&gt;
            &lt;a href="#year{{ counter }}"&gt;{{ year }}&lt;/a&gt;

            &lt;ul&gt;
                {% for month, entries in entriesInYear | group("postDate|date('F')") %}
                    &lt;li&gt;&lt;a href="{{ siteUrl }}path/to-page/{{ month }}-{{ year }}"&gt;{{ month }}&amp;nbsp;({{ entries|length }})&lt;/a&gt;&lt;/li&gt;
                {% endfor %}
            &lt;/ul&gt;
        &lt;/li&gt;

    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Then on the actual archive page (I added this at the top of the page):</p>

<pre><code>{% set month = craft.request.segment(3) %}
{% set year = craft.request.segment(4) %}

{% set firstDay = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDay = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}
</code></pre>

<p>Then where you want the info to appear:</p>

<pre><code>{% set archive  = craft.entries.section('sectionName').postDate('and', '&gt;= ' ~ firstDay, '&lt;= ' ~ lastDay) %}

&lt;ul&gt;
    {% for entry in archive %}         
        &lt;li&gt;
            &lt;h3&gt;&lt;a href="{{ entry.url }}"&gt;{{ entry.title }}&lt;/a&gt;&lt;/h3&gt;
            {{ entry.body }}

            &lt;p class="more"&gt;&lt;a href="{{ entry.url }}"&gt;read more&lt;/a&gt;&lt;/p&gt;

            &lt;div&gt;
                &lt;p&gt;{{ entry.postDate | date("d") }} &lt;span&gt;{{ entry.postDate | date("M") }}&lt;/span&gt;&lt;/p&gt;
            &lt;/div&gt;
        &lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

<p>Now here's the bit that caused the grief...  Instead of having <code>'month'-'year'</code> as part of the route, P&amp;T suggested using just <code>'*'</code>.</p>

<p><a href="https://i.sstatic.net/7w20g.jpg"><img src="https://i.sstatic.net/7w20g.jpg" alt="Route using asterisk"></a></p>

<p>Or.. as P&amp;T put it:</p>

<blockquote>
  <p>Your dynamic route is set with the Month and Year token. If you look at the list of available tokens, you will see that Month and Year follow a specific format, which the URLs you set up in your menu do not follow. Therefore, those URLs do not follow the pattern you set in the Route and you get a 404 page.</p>
</blockquote>

CC BY-SA 3.0
2015-10-05T11:19:50.733Z
2015-10-05T17:17:05.403Z
2015-10-05T17:17:05.403Z
Martin
<p>With a bit of looking around i've got the following code on my News landing page:</p>

<pre><code>  {% set allEntries = craft.entries.section('chambersNews').limit(null) %}
  &lt;ul id="archive"&gt;
  {% for year, entriesInYear in allEntries | group("postDate.year") %}
      &lt;li&gt;&lt;a href=""&gt;{{ year }}&lt;/a&gt;
        &lt;ul&gt;
        {% for month, entries in entriesInYear | group("postDate|date('F')") %}
            &lt;li&gt;&lt;a href=""&gt;{{ month }}&amp;nbsp;({{ entries|length }})&lt;/a&gt;&lt;/li&gt;
        {% endfor %}
        &lt;/ul&gt;
      &lt;/li&gt;
  {% endfor %}
  &lt;/ul&gt;
</code></pre>

<p>Using <code>&lt;li&gt;&lt;a href=""&gt;{{ month }}&amp;nbsp;({{ entries|length }})&lt;/a&gt;&lt;/li&gt;</code>  I get the desired result (visually), see attached image.  If I use <code>&lt;li&gt;{{ entry.getLink() }}&lt;/li&gt;</code> I get the result shown on the right of the attached image.</p>

<p>In both instances I only get a link to the first segment (also shown in the attached image).</p>

<p>What I would like to do is have the Months link to a page that lists just the articles from that month (I don't need the year to link).</p>

<p>I assume I need to add a dynamic link into the href but how and what?</p>

<p>Many thanks in advance.</p>

<p>Martin</p>

<p><a href="https://i.sstatic.net/hpIi5.jpg"><img src="https://i.sstatic.net/hpIi5.jpg" alt="Need the left hand side linking through to a page showing articles from a chosen month"></a></p>

<archives>
Linking and showing archive by month
# Linking and showing archive by month

(Asked by: username_0 on 2015-09-17)

With a bit of looking around i've got the following code on my News landing page:
  {% set allEntries = craft.entries.section('chambersNews').limit(null) %}
  
  {% for year, entriesInYear in allEntries | group("postDate.year") %}
      {{ year }}
        
        {% for month, entries in entriesInYear | group("postDate|date('F')") %}
            {{ month }} ({{ entries|length }})
        {% endfor %}
        
      
  {% endfor %}
  

Using {{ month }} ({{ entries|length }})  I get the desired result (visually), see attached image.  If I use {{ entry.getLink() }} I get the result shown on the right of the attached image.
In both instances I only get a link to the first segment (also shown in the attached image).
What I would like to do is have the Months link to a page that lists just the articles from that month (I don't need the year to link).
I assume I need to add a dynamic link into the href but how and what?
Many thanks in advance.
Martin

**Tags:** archives

---

## 3 answers

### Answer by username_0 on 2015-10-05. Score: 9 | Accepted answer

So P&T chipped in to help and of course solved my plight immediately :D
For anyone else who might encounter a similar scenario I'm posting the solution.
On the page containing the archive list:
{% set allEntries = craft.entries.section('sectionName').limit(null) %}


    {% for year, entriesInYear in allEntries | group("postDate.year") %}

        {% set counter = ( counter | default(0) ) + 1 %}

        
            {{ year }}

            
                {% for month, entries in entriesInYear | group("postDate|date('F')") %}
                    {{ month }} ({{ entries|length }})
                {% endfor %}
            
        

    {% endfor %}


Then on the actual archive page (I added this at the top of the page):
{% set month = craft.request.segment(3) %}
{% set year = craft.request.segment(4) %}

{% set firstDay = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDay = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

Then where you want the info to appear:
{% set archive  = craft.entries.section('sectionName').postDate('and', '>= ' ~ firstDay, '<= ' ~ lastDay) %}


    {% for entry in archive %}         
        
            {{ entry.title }}
            {{ entry.body }}

            read more

            
                {{ entry.postDate | date("d") }} {{ entry.postDate | date("M") }}
            
        
    {% endfor %}


Now here's the bit that caused the grief...  Instead of having 'month'-'year' as part of the route, P&T suggested using just '*'.

Or.. as P&T put it:

Your dynamic route is set with the Month and Year token. If you look at the list of available tokens, you will see that Month and Year follow a specific format, which the URLs you set up in your menu do not follow. Therefore, those URLs do not follow the pattern you set in the Route and you get a 404 page.


---

### Answer by username_1 on 2015-09-21. Score: 1

The code on the left is what you want to use... Here are the general steps to follow to put everything together.
Since you're creating an archive page, you have to explicitly tell Craft what to do. First step then is to set the URL to something like:
href="/chamber-news/archive/{{ month }}/{{ year }}"

Then set up a template (my example uses one named "chamber-archive") which looks to the URL for the month & year to determine which entries to load. (altered code borrowed from this answer)
{% set month = craft.request.segment(3) %}
{% set year = craft.request.segment(4) %}

{% set firstDay = now | date_modify('first day of ' ~ month ~ ' ' ~ year ~ ' 00:00:00') %}
{% set lastDay = now | date_modify('last day of ' ~ month ~ ' ' ~ year ~ ' 23:59:59') %}

{% set archive  = craft.entries.section('sectionNameHere').postDate('and', '>= ' ~ firstDay, '<= ' ~ lastDay) %}

Then you need to set up routing to tell Craft to use this template for this URL pattern.

> **Comments:**
> **username_0**: Sorry, I can't get this to work.  I've followed your instruction but it's just loading the _layout template :(  
> **username_2**: Small note to this: You'd be able to simply call the `month` and `year` tokens as `{{ month }}` and `{{ year }}`, instead of manually setting them. Craft passes your tokens to route templates as their variable, per [documentation](http://buildwithcraft.com/docs/routing#dynamic-routes).  
> **username_1**: Very good to know @TaylorDaughtry. Thank you for adding this!  
> **username_1**: @martin Show me screenshots of each piece that you've completed please. Does it appear that just the routing part isn't working?  
> **username_0**: So I have the following so far... https://i.sstatic.net/bzGsQ.jpg  I'm not sure if it has any bearing but i'm also using this method for directing Structure entries to their own templates.  http://craftcms.stackexchange.com/questions/96/how-can-i-route-a-specific-entry-in-a-structure-section-to-its-own-template  Many thanks  Martin  
> **username_1**: Try adding "Hello World" to the news-archive.twig template to see if it's loading but just not showing entries... That will help you decide in which direction to troubleshoot. Also, set the URL in chamber-news.twig to start with a slash at the beginning. Additionally, print out {{ month }} and {{ year }} in news-archive.twig to ensure the variables are getting loaded correctly. You may need to add a slash at the start of the route URL as well.  
> **username_1**: Also, you can use {{ month | lower }} when creating the URL to make the month name lower case.  
> **username_0**: I've tried the "Hello World" thing and it doesn't show.  I've also tried the slash.  In the Docs it says not to use a slash at the beginning of a route.  I know it's only loading the _layout template because the page that does load just contains the _layout info.  Curiously I tried this and it does load the template http://cliveportman.co.uk/code/archives-by-month-using-craft-cms, however it still doesn't show any content!? :(  
> **username_1**: That example is pretty much the same method so use that code if you have it working.  
> **username_0**: Thanks... but although it shows the right template, it doesn't show any content :/  
> **username_1**: Please post the contents of your template somewhere.  

---

### Answer by username_3 on 2015-09-22. Score: 0

Why not use getUrl instead of getLink? 
{{ month }} ({{ entries | length }})


---

