[英]Filtering content based on first parameter in array in Jekyll
In my posts' Front Matter, I have "categories" which is an array. 在帖子的“前题”中,我有一个“类别”,它是一个数组。
I'm looking for a way to filter the posts based on the first element in the categories array. 我正在寻找一种基于Categories数组中的第一个元素过滤帖子的方法。
For example, if I had two posts' Front Matter like: 例如,如果我有两个帖子的“前题”,例如:
title: Post Number One
categories:
- first post ever
- cool stories
and 和
title: Post Two
categories:
- cool stories
I want a way to filter on categories where "cool stories" would return only "Post Two" because "cool stories" shows up as the first element of the array. 我想要一种过滤类别的方法,其中“很酷的故事”将仅返回“后两个”,因为“很酷的故事”显示为数组的第一个元素。
There are several ways to implement this feature. 有几种方法可以实现此功能。 One of which is: 其中之一是:
Create a new include file in _includes
named first-category.html
with the following code: 使用以下代码在名为first-category.html
_includes
创建一个新的包含文件:
{% assign chosen_category = include.category %}
{% for post in site.posts %}
{% for category in post.categories.first %}
{% if category == chosen_category %}
{{ post.title }}
{% endif %}
{% endfor %}
{% endfor %}
Then, in the page where you're listing the post which have the first category as the one in question, simply include the above file and pass the chosen category name: 然后,在您列出该帖子的页面中,该页面的第一个类别为相关类别,只需添加上述文件并传递所选的类别名称即可:
## Post that have the first category of "cool stories"
{% include first-category.html category = "cool stories" %}
## End
The above code will only show posts which have "cool stories" as the first category in the posts' front-matter. 上面的代码将仅显示具有“很酷的故事”作为帖子的第一类的帖子。
This is an Information Architecture (IA) question. 这是一个信息体系结构(IA)问题。
Let's use Jekyll's category/categories inner working to represent our IA. 让我们使用Jekyll的类别内部类别来表示我们的IA。
If you define a post like this : 如果您定义这样的帖子:
---
title: "My post"
category: "main category"
categories:
- other
- wat!
# ... more front matter variables
---
Category/categories will be available as : 类别将以:
post.category => main category
post.categories =>
- other
- wat!
- main category
Now if you want to use category
to filter your posts, using group_by and where_exp filters , you can do : 现在,如果要使用category
来过滤帖子,请使用group_by和where_exp过滤器 ,可以执行以下操作:
{% assign category = "main category" %}
{% comment %} #### Grouping posts by 'main' category {% endcomment %}
{% assign grouped = site.posts | group_by: "category" %}
{{ grouped | inspect }}
{% comment %}#### Get our category group{% endcomment %}
{% assign categoryPosts = grouped | where_exp: "group", "group.name == category" | first %}
{{ categoryPosts | inspect }}
{% comment %} #### All interesting posts are now in categoryPosts.items {% endcomment %}
{{ categoryPosts.items | inspect }}
{% comment %} #### We can now sort and loop over our posts {% endcomment %}
{% assign sorted = categoryPosts.items | sort: "whateverKeyYouWantToSortOn" %}
<ul>
{% for post in sorted %}
<li>
<a href="{{ site.baseurl }}{{ post.url }}">{{ post.title }}</a>
<br>Category : {{ post.category }}
<br>Categories :
<br><ul>{% for c in post.categories %}
<li>'categorie' {{ forloop.index }} - {{ c }}</li>
{% endfor %}</ul>
</li>{% endfor %}
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.