7

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) %}
  <ul id="archive">
  {% for year, entriesInYear in allEntries | group("postDate.year") %}
      <li><a href="">{{ year }}</a>
        <ul>
        {% for month, entries in entriesInYear | group("postDate|date('F')") %}
            <li><a href="">{{ month }}&nbsp;({{ entries|length }})</a></li>
        {% endfor %}
        </ul>
      </li>
  {% endfor %}
  </ul>

Using <li><a href="">{{ month }}&nbsp;({{ entries|length }})</a></li> I get the desired result (visually), see attached image. If I use <li>{{ entry.getLink() }}</li> 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

Need the left hand side linking through to a page showing articles from a chosen month

9

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) %}

<ul id="archive">
    {% for year, entriesInYear in allEntries | group("postDate.year") %}

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

        <li class="year{{ counter }}">
            <a href="#year{{ counter }}">{{ year }}</a>

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

    {% endfor %}
</ul>

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) %}

<ul>
    {% for entry in archive %}         
        <li>
            <h3><a href="{{ entry.url }}">{{ entry.title }}</a></h3>
            {{ entry.body }}

            <p class="more"><a href="{{ entry.url }}">read more</a></p>

            <div>
                <p>{{ entry.postDate | date("d") }} <span>{{ entry.postDate | date("M") }}</span></p>
            </div>
        </li>
    {% endfor %}
</ul>

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

Route using asterisk

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.

| improve this answer | |
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.

enter image description here

| improve this answer | |
  • Sorry, I can't get this to work. I've followed your instruction but it's just loading the _layout template :( – Martin Sep 22 '15 at 11:57
  • 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. – Taylor Daughtry Sep 22 '15 at 15:00
  • Very good to know @TaylorDaughtry. Thank you for adding this! – Anna_MediaGirl Sep 22 '15 at 16:09
  • @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 Sep 22 '15 at 16:13
  • So I have the following so far... i.stack.imgur.com/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. craftcms.stackexchange.com/questions/96/… Many thanks Martin – Martin Sep 22 '15 at 20:12
0

Why not use getUrl instead of getLink?

<li><a href="{{ entry.getUrl() }}">{{ month }} ({{ entries | length }})</a></li>
| 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.