【问题标题】:Get Jekyll Posts by Year [duplicate]按年份获取 Jekyll 帖子 [重复]
【发布时间】:2014-08-02 11:25:51
【问题描述】:

如果您愿意,我正在制作我网站上所有帖子的“存档”。我想收集一年的所有帖子。但是,此代码可以正常工作;我希望它在需要时生成<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 %}

【问题讨论】:

    标签: html ruby jekyll


    【解决方案1】:

    怎么样:

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

    【讨论】:

    • 液体异常:journal/index.html 中的未知标签“年份”
    【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 2014-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多