7
votes

How do I loop through a Jekyll site's posts but only take action on posts where the year is equal to a specific value?

{% for post in site.posts %}
  {% if post.date.year == 2012 %}
      <p>{{ post.date }}</p>
      <p>{{ post.title }}</p>
  {% endif %}
{% endfor %}

The above does not work. What is the correct way to do this?

1

1 Answers

8
votes

To extract the year of a date, you have to use the date filter with "%Y" (the full syntax is listed here). i.e.:

{% for post in site.posts %}
  {% capture year %}{{post.date | date: "%Y"}}{% endcapture %}
  {% if year == "2012" %}
      <p>{{ post.date }}</p>
      <p>{{ post.title }}</p>
  {% endif %}
{% endfor %}