colmulhall
colmulhall

Reputation: 1736

Posts by month in Jekyll

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.

Upvotes: 2

Views: 1288

Answers (1)

rocambille
rocambille

Reputation: 15996

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.

Upvotes: 5

Related Questions