DSS
DSS

Reputation: 53

How do I define a custom collection in Jekyll?

Following the Jekyll Collections documentation, I wrote the following code in _config.yml

_config.yml

collections: 
- popular_posts

So when I print {{ site.collections }}, the output is "popular_posts".

I also made a folder called "_popular_posts" at the same level as "_posts". _popular_posts contains two .md files with some YAML front matter, same as a post.

However, if I print {{ site.popular_posts }} or {{ site.collections.popular_posts }}, there is no output.

How do I have Jekyll recognize the .md files in that directory so that the following code will work?

{% for popular_post in site.popular_posts %}
  <a href="{{ popular_post.link }}">
    <h1>{{ popular_post.title }}</h1>
    <img class="pop-img" src="{{ popular_post.image_url }}">
  </a>
  <span id="pop-order"><span class="pop-current-popular_post-number">{{ popular_post.number }}</span>/5</span>
{% endfor %}

Upvotes: 5

Views: 2481

Answers (1)

parkr
parkr

Reputation: 833

It's quite easy! You're on the right track. In your _config.yml:

collections:
- popular_posts

This will tell Jekyll to read in all the files in _popular_posts.

If you want each of those two files to have a corresponding output file (like how _posts works now), you'll want to change your _config.yml to:

collections:
  popular_posts:
    output: true

This will produce files at /popular_posts/filename1.html and /popular_posts/filename2.html, one page for each post.

Collections are only recently up on GitHub Pages so if you were trying this there, it would have failed.

Check out jekyll-help for more help if you need it!

Upvotes: 6

Related Questions