Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jekyll loops for posts and other section

Tags:

ruby

jekyll

blogs

I use Jekyll for more than 3 month now. I have made several blogs with it, but I have one question I couldn't find the answer anywhere.

In order to render all your posts , all the markdown files in the _posts, I use a for loop like this one for instance :

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

I want to do the same with my projects. I created a _projects folder and tried to render them by using :

{% for project in site.projects %}
   {{project.title}}
{% endfor %}

But Jekyll doesn't seem to recognize the _projects folder. What should I do to get the same results ?

like image 319
MaximeHeckel Avatar asked Mar 23 '23 15:03

MaximeHeckel


2 Answers

This is not the way custom post types work in Jekyll. You can however put a _posts directory in another directory and build custom categories this way.

Suppose you would have your projects organized under projects/_posts, then your template would have to look something like this:

{% for post in site.categories.projects %}
  {{ post.title }}
{% endfor %}

Found it in Jekyll's Github Issues

like image 118
Patrick Oscity Avatar answered Apr 05 '23 11:04

Patrick Oscity


Now we have Jekyll Collections

"Add the following to your site’s _config.yml file, replacing my_collection with the name of your collection."

For example you should add:

collections:
- projects

Then of course, you could use it in your template in the most easy way:

{% for project in site.projects %}
   {{project.title}}
{% endfor %}
like image 37
konus Avatar answered Apr 05 '23 10:04

konus