获得Jekyll年度帖子

时间:2014-06-12 08:01:32

标签: html ruby jekyll

如果您要访问我网站上的所有帖子,我正在制作“存档”。我想收集一年的所有帖子。但是,此代码工作正常;我希望它在需要时生成<h2>2014</h2>

基本上,如果年份是2014年,请提交 <h2>2014</h2> 并在所有帖子中生成 <ul> (包含期刊类别) )从那一年开始。

如果有人知道按年份存档的任何.rb插件,请告诉我们!

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

3 个答案:

答案 0 :(得分:0)

怎么样:

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

答案 1 :(得分:0)

您可以在帖子中添加年份字段,然后

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

答案 2 :(得分:0)

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

相关问题