【问题标题】:How to show first post from category in Jekyll with Liquid如何在 Jekyll 中使用 Liquid 显示类别中的第一篇文章
【发布时间】:2014-07-14 16:56:11
【问题描述】:

我找不到解决方案。我有三个类别:tuts、news、code。

最新的帖子归类在 tuts 中。但我想在 news 中显示最后和最新的帖子。我尝试了以下操作,但显然它没有显示任何内容,因为如果我将循环限制为第一项,即 tuts 项,则循环停止。

{% for post in site.posts limit:1 %}
    {% if post.categories contains 'news' %}
        <a href="{{ site.url }}/news/">NEWS</a></strong> › <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>
    {% endif %}
{% endfor %}

如何显示特殊类别的第一个帖子?我可以像这样直接循环选择一个类别吗?如果是,正确的语法是什么?

{% for post in site.posts.categories.news limit:1 %}
        <a href="{{ site.url }}/news/">NEWS</a></strong> › <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>
{% endfor %}

【问题讨论】:

    标签: jekyll liquid


    【解决方案1】:

    是的,可以直接循环某个类别或标签的所有帖子。

    只是:

      {% for post in site.categories['news'] limit:1 %}
        <a href="{{ post.url }}">{{ post.title }}</a>
      {% endfor %}
    

    标签也一样,只需要将site.categories['news']替换为site.tags['news']即可。

    【讨论】:

    • 有什么方法可以有效地处理多个标签?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2011-07-13
    • 1970-01-01
    相关资源
    最近更新 更多