You shared what you did and asked what you are missing, but I am unclear about the problem. Are you saying Jekyll does not build because you are receiving errors?
In any case, let me see if I can help you with structuring your folders and at the end I share how to use the single collections folder (despite my not having used it before) because I think your configuration may be a little off-bok from the Jekyll docs. For example, you have code written as collections_dir: ['collections']
when I think it should be collections_dir: 'collections'
(no brackets).
Hopefully, this article will help you with structuring your site and accessing your content.
Folder structure
Moving your collections aside, creating a folder structure with dates is cumbersome and probably not worth your while. When you create your posts in the _posts
folder, like this:
Jekyll will output the files into a date-based layout structure, like this:
That means you do not have to create a date-based folder structure for the posts you write unless it is something you just feel like doing, in which case, go for it.
If you want to change how Jekyll outputs the folder structure for your site, tailor the permalinks in your ./_config.yml
file.
Press vs Blogs
Having a press
and blog
folder structure under _posts
is valid (I’m pretty sure). If your press releases display the same exact way as a blog post, then that is fine, but I suspect you might want to at least filter them.
The following options show you how to decide whether you want to display all posts (post+press), or just posts, or just press posts:
Option #1: Jekyll front matter
Create a custom postType
element in your front matter. Also, you could use the built-in categories
or tags
, however, you might find them useful for other scenarios as you design your blog. That is why I like to create a custom front matter element, but you can use the same code below and change the where
filter.
Here is what a press post might look like:
/_posts/2022-01-01-my-press-release.md
---
layout: post
date: 2022-01-01
postType: press
title: My press release
---
For immediate announcement...
Note that in my example I use the layout: post
, but you could easily create a new layout that is unique to a press release.
Here is what a blog post might look like
/_posts/2022-01-02-hello-world.md
---
layout: post
date: 2022-01-02
postType: blog
title: Hello World!
---
Welcome to my first blog post about things and stuff...
You could, if you want, still put the posts in a _posts/posts
folder and the press releases in a _posts/press
folder to make it easier for yourself, but I suggest you don’t bother yourself by creating date folder structures since you can easily sort the file list since the dates are in the names. Whether you use those subfolders or not, all the posts will be in the posts
collection.
Here is how you would display the title of everything in the _posts
folder (posts+press):
{%- assign allPosts = site.posts -%}
{% for post in allPosts %}
Post title: {{post.title}}
Post type: {{post.postType}}
Whatever other html or markdown you need...
{% endfor %}
Here is how you only display posts:
{%- assign blogPosts = site.posts | where:"postType","blog" -%}
{% for post in blogPosts %}
Blog title: {{post.title}}
Post type: {{post.postType}}
Whatever other html or markdown you need...
{% endfor %}
And finally, here is how you would display press:
{%- assign pressPosts = site.posts | where:"postType","press" -%}
{% for post in pressPosts %}
Press title: {{post.title}}
Post type: {{post.postType}}
Whatever other html or markdown you need...
{% endfor %}
Option #2: Collections
In this scenario, I would just keep your blog posts in the _posts
folder and use Jekyll’s built-in post
collection and then create a new press
collection.
First, you modify your /_config.yml
file by adding these lines:
collections:
press:
output: true
Note: If you want to use this feature and you are testing locally, you will have to start and stop Jekyll to use your new collection.
Next, create the folder ./_press
, which will be the folder you use to add press posts.
In the ./_press
folder, you would create all your press posts. Note that you do not have to use the same file structure with the dates.
Here is what a press
would look like in the _press
folder:
./_press/my-press-release.md
or
./_press/2022-01-01-my-press-release.md
(the filename is up to you)
---
layout: press (or post or whatever layout you want to use)
date: 2022-01-01
title: My press release
---
For immediate announcement...
To access posts, you use the post
collection, like this:
{%- assign allPosts = site.posts -%}
{% for post in allPosts %}
Post title: {{post.title}}
Whatever other html or markdown you need...
{% endfor %}
To access press, you use the press
collection, like this:
{%- assign pressPosts = site.press -%}
{% for press in pressPosts %}
Press title: {{press.title}}
Whatever other html or markdown you need...
{% endfor %}
Moving all your collections into a single collection
folder
I’m going to be upfront and tell you I never created a single collections folder, but I do see differences in your config than the official Jekyll Collections documentation. Here, I will share what I think you need to do if you want to use posts and a separate pages
collection all under a single folder.
Modify your configuration by adding the following lines:
./config.yml
collections_dir: my_collections
collections:
press:
output: true
Once that is complete, create a ./my_collections
folder.
Copy and or create the following folders into the ./my_collections
folder:
./my_collections/_posts
./my_collections/_drafts
./my_collections/_press (optional based on the option you like most above)
All the things I shared with you throughout this post still work, but all your collections will be located under the my_collections
folder.
One caveat to this approach of using a single collections folder is if you decide to use a headless CMS like Forestry, those applications may or may not recognize your tailored folder structure. Test your new structure before going all-in.