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?
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With