2

I am trying to create an archive page of posts from my website. What I would like is to be able to have a pages for each list of posts by month in this format:

www.mywebsite.com/2016/11 would display all posts for November 2016.

Can I have a page for each month I have posted that is dynamically created each time I post in a new month? I don't want to have to manually create a new page for each month.

I already can group posts by year like so:

<ul>
{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%Y" %}
  {% if currentdate != date %}
    <li id="y{{currentdate}}">{{ currentdate }}</li>
    {% assign date = currentdate %} 
  {% endif %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>

Thanks for any help.

colmulhall
  • 1,548
  • 3
  • 18
  • 33

1 Answers1

5

You can modify your date filter to integrate the month, e.g. date: "%B %Y". That's what I used in a layout, with a separate <ul> for each month.

From documentation, month-only values for the date filter are:

  • %b: abbreviated month name.
  • %B: full month name.
  • %m: month of the year (01 - 12).

Complete loop:

<ul>
{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%B %Y" %}
  {% if currentdate != date %}
    <li id="y{{currentdate}}">{{ currentdate }}</li>
    {% assign date = currentdate %} 
  {% endif %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
{% endfor %}
</ul>

About generating the pages, AFAIK this can only be done through a plugin. If you can't use plugins, e.g. if you're hosting your pages on GitHub, the best you can do is to reduce your pages to a short YAML frontmatter relying on a layout, like in this answer.

rocambille
  • 15,398
  • 12
  • 50
  • 68
  • 1
    Thanks, that helped me. I am hosting on GitHub, so I won't be using any plugins for this (and to be honest I want to keep the site as lightweight as possible, so I don't want any dependencies on external plugins). – colmulhall Nov 09 '16 at 09:14