0

I'm making an "archive" if you will of all the posts on my site. I want to gather all the posts from a year. This code works fine, however; I want it to generate the <h2>2014</h2> when needed.

Basically, if year is 2014, render <h2>2014</h2> and make a <ul> of all the posts (with category of journal) from that given year.

If anyone knows of any .rb plugins that archive by year, let me know!

<h2>2014</h2>
{% for post in site.categories.journal %}
    {% capture year %}{{post.date | date: "%Y"}}{% endcapture %}
        {% if year == "2014" %}
                <ul class="posts-in-year">
                    <li><p><a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}</a> &mdash; {{ post.date | date: "%B %d" }}</p></li>
                </ul>
        {% endif %}
{% endfor %}

I want it to look like this:

enter image description here

Matthew
  • 2,158
  • 7
  • 30
  • 52
  • Update: I just noticed that [Get Jekyll Posts by Year](http://stackoverflow.com/questions/24179283/get-jekyll-posts-by-year) is a duplicate as well. Unfortunately I can't change my flag, otherwise I would point to this question: [Jekyll/Liquid Templating: How to group blog posts by year?](http://stackoverflow.com/q/19086284/6884) – Christian Specht Jun 13 '14 at 13:52

1 Answers1

1
{% for post in site.categories.journal %}
  {% capture currentyear %}{{post.date | date: "%Y"}}{% endcapture %}
  {% if currentyear != year %}
     <h2>{{ currentyear }}</h2>
    {% capture year %}{{currentyear}}{% endcapture %} 
  {% endif %}
  <ul class="posts-in-year">
    <li><p><a href="{{ post.url | prepend: site.baseurl }}">{{ post.title }}    </a> &mdash; {{ post.date | date: "%B %d" }}</p></li>
  </ul>
{% endfor %}
Matthew
  • 2,158
  • 7
  • 30
  • 52