jekyll - 如何在 Jekyll with Liquid 中显示类别中的第一篇文章

标签 jekyll liquid

我找不到解决办法。我有三类:tuts、新闻、代码。

最新的帖子归类为 tuts。但我想显示新闻中的最后一个和最新的帖子。我尝试了以下操作,但显然它没有显示任何内容,因为如果我将循环限制为第一项,即 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 %}

最佳答案

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

只是:

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

标签也是一样,你只需要替换site.categories['news']来自 site.tags['news'] .

关于jekyll - 如何在 Jekyll with Liquid 中显示类别中的第一篇文章,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24741729/

相关文章:

ruby - 动态变量 Jekyll Liquid

variables - GitHub 页面上 Jekyll 上的 Liquid 变量的范围是什么

ruby - 从 Liquid 和 Markdown 而不是 HTML 生成 Jekyll 页面

ruby-on-rails - 找不到Jekyll主题

ruby - Jekyll 默认使用系统 Ruby 版本而不是 RVM 版本

Jekyll 显示子类别的帖子数

html - 在 .liquid 上集成 Bootstrap ( shopify )

github 页面上的 Jekyll 将除法结果显示为分数

templates - 在 Jekyll 模板中定义 site.title

javascript - 与 Shopify 一起使用 sharethis API 时不存在 'Access-Control-Allow-Origin' header