Only return Jekyll messages for a specific year

How can I scroll through entries in a Jekyll site, but take action only on messages where the year is a certain 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 right way to do this?

+6
source share
1 answer

To extract the year for a date, you must 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 %} 
+6
source

Source: https://habr.com/ru/post/910274/


All Articles