我希望能够使用Eleventy来创建父集合和子集合,然后我可以遍历这些集合来创建导航。
我目前在一个名为continents的集合中有一些帖子,前面的内容如下所示:
---
title: Europe
tags: continents
---我遍历它来创建一个链接列表:
<ul class="parent-list">
{% for post in collections.continents %}
<li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
{% endfor %}
</ul>可以有一个continents的子集吗?例如countries?如果是这样的话,这些数据需要添加到我的主题中吗?
如果能够像这样循环遍历集合,那就太好了:
<ul class="parent-list">
{% for post in collections.continents %}
<li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
<ul class="child-list">
{% for post in collections.countries %}
<li><a href="{{ post_url | post }}">{{ post.data.title}}</a></li>
{% endfor %}
</ul>
{% endfor %}
</ul>我知道eleventy-navigation,但它看起来你也只能有一个级别的导航。
发布于 2019-12-05 00:40:03
据我所知,集合只有一层深,所以你不能做collections.parent.child。您可以动态创建集合,这样您就拥有了collections.europe和collections.northamerica。然后,您可以将第二个内部循环切换为如下所示:
{% for post in collections.countries[post.data.title] %}这有意义吗?
我要补充的是,为了让它起作用,你的子帖子应该使用前面的内容来设置它们的父帖子,比如:continent: europe
https://stackoverflow.com/questions/59074595
复制相似问题