Jekyll仅在页面上显示特定标签

时间:2015-11-11 09:59:24

标签: ruby jekyll

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

感谢。

2 个答案:

答案 0 :(得分:3)

您可以通过迭代网站标记并使用标记名称索引到数组中来执行此操作,类似于以下内容:

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

答案 1 :(得分:1)

@briantist是对的,你必须深入了解site.tags集合。

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

标记页面(例如:ruby.html)只是调用一个include,传递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 %}

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