Get Jekyll Posts by Year [duplicate]

北慕城南 提交于 2019-12-30 14:50:42

问题


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 %}

回答1:


What about:

{% for post in site.categories.journal %}
    {% capture year %}{{post.date | date: "%Y"}}{% endcapture %}
        {% if year == "2014" %}
                <h2>{% year %}</h2>
                <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 %}



回答2:


You can add a year field in your posts and then

<h2>2014</h2>
<ul class="posts-in-year">
  {% for post in (site.categories.journal | where: "year","2014" | sort: 'date') %}
    <li><p><a href="{{ post.url | prepend: site.baseurl }}">
      {{ post.title }}</a> &mdash; {{ post.date | date: "%B %d" }}</p>
    </li>
  {% endfor %}
</ul>



回答3:


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



来源:https://stackoverflow.com/questions/24179283/get-jekyll-posts-by-year

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!