Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only return Jekyll posts that match specific year

Tags:

jekyll

liquid

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?

like image 378
kindohm Avatar asked Mar 08 '12 19:03

kindohm


1 Answers

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 %}
like image 99
huon Avatar answered Nov 30 '22 07:11

huon