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:
/about
/work
/connect

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.

Hi,
I have tried the same as you mentioned. But didn’t worked out.

  • MyBlogContent is a subfolder inside posts.

layout: default

Blog

{% for post in site.category.MyBlogContent %}
    <li>
        <a href="{{ post.url }}">{{ post.title }}</a>
    </li>
    {% endfor %}
</div>

and my .md file is

---

layout: blogLayout
category: MyBlogContent

Stoker’s handwritten notes on the characters in the novel
The story is told in epistolary format, as a series of letters, diary entries, newspaper articles, and ships’ log entries, whose narrators are the novel’s protagonists, and occasionally supplemented with newspaper clippings relating events not directly witnessed. The events portrayed in the novel take place chronologically and largely in England and Transylvania during the 1890s and all transpire within the same year between 3 May and 6 November. A short note is located at the end of the final chapter written 7 years after the events outlined in the novel.

this may need to be site.categories