【问题标题】:Jekyll show only specific tag on pageJekyll 仅在页面上显示特定标签
【发布时间】:2015-11-11 09:59:24
【问题描述】:

我正在使用 jekyll,并且我正在尝试让一个页面 (/play) 仅显示标记为 Play 的帖子,并且与 Work (/work) 相同。我该怎么做?

谢谢。

【问题讨论】:

标签: ruby jekyll


【解决方案1】:

您可以通过迭代站点的标签并使用标签名称索引到数组中来做到这一点,类似于以下内容:

{% for post in site.tags.Play %}
<h2>{{ post.title }}</h2>
<time>{{ post.date }}</time>
{% endfor %}

这有点令人困惑,因为site.tags 似乎返回了一个标签集合,但它实际上包含了完整的帖子,由标签索引。

See site.tags.TAG in the Jekyll Variables page.

【讨论】:

    【解决方案2】:

    @briantist 是对的,你必须在 site.tags 集合中挖掘。

    为了获得更易于维护的代码,您可以使用Jekyll includes

    一个标签页(例如:ruby.html)只是调用了一个包含,将 ruby​​ tagName 传递给它:

    ---
    layout: page
    title: Ruby
    ---
    
    {% include tagPagesLoop.html tagName='ruby' %}
    

    _includes/tagPagesLoop.html

    <h2>All {{ include.tagName }} posts</h2>
    
    {% for post in site.tags[include.tagName] %}
    <h3>{{ post.title }}</h3>
    other stuff here
    {% endfor %}
    

    在此包含中所做的所有更改都存在于使用它的所有页面中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-31
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2018-08-14
      相关资源
      最近更新 更多