Display posts from current month only?

Hey folks,

I have a need to display posts from the current month only. I can see lots of guides on how to create an archive page that lists posts by month. But these all show all posts. I just want to show the current month.

So far I have the follow snippet of code that shows all the posts on my site by month. Does anyone know how I can tweak this code to only show the current month please?

{% for post in site.posts %}
  {% assign currentdate = post.date | date: "%B %Y" %}
  {% if currentdate != date %}
  <h3>{{ currentdate }}</h3>
    {% assign date = currentdate %}
  {% endif %}
  <strong><a href="{{ post.url }}">{{ post.title }}</a></strong><br>
  <em>{{ post.description }}</em>
{% endfor %}

I’m sure it’s very simple, I’m just struggling to work out how to filter the results.

Thanks in advance!

Working with dates in Jekyll can be frustrating. Jekyll does offer group_by and group_by_exp filters. However, I struggle with matching dates like you are requesting. It may be possible, but I do not have that answer for you.

However, I did come up with what I believe is a working solution. I only did a quick test, so let me know if it works with your setup.

Only list posts for the “current month” (please read the site.time section below before implementing this)

{% assign currentMonth = site.time | date: '%Y %b' %}
Current month: {{currentMonth}}
{% assign posts = site.posts %}
{% for post in posts %}
    {% assign postDate = post.date | date: '%Y %b' %}
    {% if postDate == currentMonth %}
        Title: {{post.title}}
    {% endif %}
{% endfor %}
  • The assign currentMonth creates a string based on the current site.time (more on that later) and uses the date filter to convert it to something readable. In this case, the output will be something like:

2022 Jan

  • The assign posts creates an array of all the posts.

  • The for post loop traverses the array of posts.

  • The assign postDate creates a string based on the post’s post.date) and uses a date filter to convert it to something readable. In this case, the output will be something like:

2022 Jan

  • The if postDate statement compares currentMonth with postDate. If the two matches, display the post’s title (or whatever other information you like).

Important note on site.time

Jekyll is a static site, which means that site.time is not today’s date, but the last time you built your Jekyll site.

Let’s say you wrote a post or made some changes to your site in December 2021. Those changes will automatically trigger a rebuild of your site, and your site.time will be December 2021.

Now let’s say you did not make any changes in January. Your site.time is still December 2021. The reason for that is that you never made a change to your site or forced a rebuild.

To address that problem, you have a few options:

  1. Make even a slight change to your site to force a rebuild and do that at least monthly. You could create a new post, edit a file, create a new file… just make a change, and Jekyll will rebuild automatically on most platforms.

  2. Create a workflow (GitHub Action, CI/CD GitLab workflow, etc.) that automatically builds your site at least at the beginning of every month. I will not get into how to do that here, so if you want more details, please search this site for ways to do that or ask a new question.

group_by_exp

I played around with where_exp and group_by_exp to accomplish your request but did not have time to dig into it without running into date conversion issues. There might be a more elegant solution using those expressions, so if you are interested, check out the following thread:

Hi Bill,

Thanks for this, it’s really helpful and getting me started down this path. I really appreciate the very detailed response - this is a massive help.

The build time isn’t a problem as the point of this snippet is to generate some HTML for another purpose; it won’t actually be displayed on my site, so I’ll be running the build to generate the links for that month regularly anyway.

Thanks again,

Kev

1 Like

Grea! I am glad this solution is working for you!