遍历Jekyll中的子类别 [英] Iterate over subcategories in Jekyll
问题描述
在Jekyll中,帖子可以具有多个类别.例如,意大利面的食谱可能具有类别dinner
和food
.是否可以在没有插件的情况下迭代类别(食物)的其他类别(不同的晚餐时间)?例如,我要为类别food
生成以下页面:
In Jekyll, a post can have multiple categories. For example, a recipe for spaghetti might have the categories dinner
and food
. Is it possible--without plugins--to iterate over the other categories (different dinner times) of a category (food)? For example, I want to generate the following page for the category food
:
Dinner:
* Spaguetti
* Meatloaf
Breakfast
* Cheerios
* Oatmeal
Lunch
* BLT
推荐答案
就像许多Jekyll的工作一样,答案是通过循环方式超出您的预期:)
Like a lot of Jekyll work, the answer is to loop through stuff way more than you think you should :)
只需遍历所有类别或标记或整个网站的任何内容,并使用if
标记可避免输出本文中没有的内容.
Just loop through all categories or tags or whatever for your entire site, and use an if
tag to avoid outputting ones that this post doesn't have.
然后在循环正文中,再次使用if
标记循环浏览站点上的每个帖子,以避免输出不具有该类别的帖子.
Then inside the loop body, loop through every post on your site, using if
tags again to avoid outputting the ones that don't have the category.
这里有一些代码将对标签执行此操作,我认为如果将tags
替换为categories
,它将起到相同的作用.我已经从我自己的网站上对其进行了轻微修改,如果有错字或两个错字,我们对此表示抱歉:
Here's some code that will do it for tags, I think if you replace tags
with categories
it'll work the same. I've lightly modified it from my own site, sorry if there's a typo or two:
{% for topic in site.tags | sort_by:topic order:ascending %}
{% if topic == whatever_topic_you_have %}
<section class="topic">
<h1><a name="{{ topic }}">{{ topic }}</a></h1>
{% for item in site.posts %}
{% if item.tags contains topic %}
...show your post/item here...
{% endif %}
{% endfor %}
</section>
{% endif %}
{% endfor %}
这篇关于遍历Jekyll中的子类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!