Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter posts by category in Jekyll

Tags:

jekyll

I have a folder of markdown files each with a number of key/values. I need to filter all of the markdown files in the _faq folder by the key faq_category.

I have tried:

 {% assign post = site.faqs | where: "faq_category", name-of-category %}
  <ul>
  <li>{{ post.title }}</li>
  </ul>

However, this is showing nothing in the end.

The folder structure it should be looping through is:

jekyll
|
 --faqs
   |
   --name-of-faq
   --name-of-faq-2

Sample markdown file:

title: name of faq
faq_id: 2567
slug: title-of-faq
created: Mar 6, 2017
modified: Mar 6, 2017
faq_category: how to fly
like image 883
Matt Avatar asked Mar 16 '17 19:03

Matt


1 Answers

Instead of site.faqs use site.posts to get an array of posts.

Then put the markdown files in the folder: /faqs/_posts/ for example: /faqs/_posts/faq1.md.

After that you should be able to browse them like:

{% for post in site.posts %}
{{post.title}}
{% endfor %}

To filter a specific category use: site.categories.CATEGORY or filter them like: (for example the category "mycategory")

<ul>
{% for post in site.faqs %} 
{% if post.categories contains "mycategory" %}
 <li>{{ post.title }}</li> 
{% endif %}
{% endfor %}

like image 51
marcanuy Avatar answered Oct 18 '22 22:10

marcanuy