Writing a loop to display posts in a subfolder


So I’m working to build a design portfolio website using jekyll and have ran into a problem.

I have structured the site to have subfolders for each main page so there are subfolders for:

Inside the /work folder I have created a _posts folder where I plan to write posts about individual design projects.

The problem is that the for loop does not return any results. Here’s the basic loop that I have.

{% for posts in site.categories.work %}

{% endfor %}

If i change the loop to read {% for posts in site.posts %} it works.

It is my understanding that Jekyll automatically treats the site subfolders as categories, yet I can’t figure out how to make this work. I’ve used the exact same loop in a different site environment and it worked without issue.

What am I missing?


Not 100% certain, but I think Jekyll only treats subfolders as “categories” when they’re placed in _posts/.

└── _posts
   └── work
   └── another-category

You’re doing it the other way around by sticking a _posts folder within the root category folders. If you want to stick with that you can manually assign a category name to the posts with YAML Front Matter.

Adding category: work to your .md inside of /work/_posts/ should do the trick. You could also assign it to all the files in that folder using front matter defaults in your _config.yml.


EDIT: I found out what the problem was. I accidentally dated the post in the future, so Jekyll.
Posts now made in the _posts folder beneath the work folder are appearing in the loop.

This behavior must have changed recently because the previous site I was experimenting with used jekyll 3.3.1 and it supported the _posts inside a site subfolder.

Putting the folders inside _posts works but i still have to manually add the category in the front matter for them to show.

Thanks for the help.