简体   繁体   中英

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.

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:

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

  • any post must be categorized in a main category
  • post can be categorized in more than one " categorie "

Let's use Jekyll's category/categories inner working to represent our 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM