Posts not generated in custom collection directory

I’m encountering a challenge with generating posts. After creating a custom collection directory and moving _posts inside there, posts are no longer generated and can’t be accessed from site.posts.

I have a Jekyll-generated website with the following structure:

collections
  _posts
    press
        2021
          xxxx-xx-xx-title.md
          xxxx-xx-xx-title.md
        2022
          xxxx-xx-xx-title.md
          xxxx-xx-xx-title.md
   blogs
        2021
          xxxx-xx-xx-title.md
          xxxx-xx-xx-title.md
        2022
          xxxx-xx-xx-title.md
          xxxx-xx-xx-title.md

This means collections is the custom directory I created to store all collections. I moved _posts into this directory following the documentation.

Inside _posts are folders press and blogs. The purpose here is to separate the two types posts. Inside each folder, I have grouped the posts by years. So 2021 and 2022 are the folders that store posts.

Here is my configuration file:

gems:
- jekyll-paginate-v2
- jekyll-tagging
- jekyll-paginate-tags

include: ['_pages']

collections_dir: ['collections']
collections:
  drafts:
    output: false
  posts:
    output: true

defaults:
# current press and blogs
  - scope:
      path: "collections/_posts/press/"
    values:
      permalink: "/:title/"
      category: "press_en"
  - scope:
      path: "collections/_posts/blogs/"
    values:
      permalink: "/:title/"
      category: "blogs_en"

pagination:
  enabled: true
  debug: false
  per_page: 8
  title: ':title'
  sort_field: 'date'
  sort_reverse: true
  trail:
   before: 2
   after: 2

tag_page_layout: tags
tag_page_dir: '/subject'
tag_permalink_style: pretty

What am I missing here in configuration? Help is much appreciated!

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.

:file_folder: 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.

:vs: 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 %}

:arrow_heading_down: 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.

@BillRaymond Thank you for the response and attemping to help, but could you elaborate on which part of the problem you don’t understand?

I believe the problem here is explicitly stated in the title and at the beginning of this post: “Posts are not generated in custom collection directory”. What I’m showing here is what I’ve done, and it is NOT generating the posts in _posts after declaring a custom collections_dir collections.

To be clearer with the problem:

I wish to create a custom collections_dir called collections and store all collections there (including _posts) because I will create a lot more collections and don’t want them to clutter the root directory. I’m using front matter defaults to set custom paths for press and blogs.

However, after moving _posts inside collections and set the front matter defaults, I got no posts generated from _posts. This is the problem. I’m looking for solutions specific to this scenario. I believe the problem lies in how front matter defaults and collections are configured, but I can’t see the problem.

I saw no error while generating the site, to be clear. The only issue is that no post is generated with this configuration.

Thank you @BillRaymond for support. I’ve removed the brackets in collections_dir and renamed it to contents from collections. Although Jekyll displayed no error when setting collections_dir with brackets, I believe it could be the error behind the inability to generate posts from _posts. I also renamed the collection directory name to something other than collections, I don’t know if it is possible, but it could have confused Jekyll using a keyword as folder name. Anyways, the ticket is now solved and closed.

Another learning is that the front matter defaults could be wrong in the post. paths should be with the collection directory rather than root directory. With the custom collection directory named contents. The path attribute should be folders inside contents such as _posts/press rather than contents/_posts/press. This could be the other error causing this problem in the post.

1 Like

Glad you got it figured out. Sorry about the confusion with my request regarding further explaining the solution. I was not quite sure how far you got along and what you may have tried to address the problem. I appreciate the follow up and that the problem is solved!