5

I'm currently working on my first Jekyll site. I have a page in which 2 seperate sections display previews of certain posts (a styled thumbnail).

What I want is for these sections to display posts from different subfolders in _site/_posts. I have organized _posts to contain two subfolders, /lesplannen and /verslagen (lesson plans and reports), each containing a bunch of articles.

My code for one of these sections is as follows (Jade formatting):

{% for post in site.posts limit: 4 %}
    a(href="{{post.url}}" target="_blank" style="background-image: url(assets/img/posts/lesplannen/thumbnails/{{post.thumbnail}})").lesplan-thumb
      .article-meta
        .name {{post.title}}
        .date {{ post.date | date: "%b %d, %Y" }}
    {% endfor %}

And similarly for the other section. My first instinct was to change site.posts to site.posts.lesplannen and site.posts.verslagen repectively, but this resulted in the thumbs not displaying at all. I also added the categories to the front matter in the articles themselves, but this wasn't picked up either.

For clarity, what I'm trying to do is have section 1 display posts from _site/_posts/subfolder 1 and section 2 display posts from _site/_posts/subfolder 2.

What would be the right way to go about this?

Question for bonus points: The page displays 4 posts, as expected, but chooses to display post 6 through 9 of the 12 posts available. Why is this and can I specify to e.g. always display the last x posts?

For reference:
GitHub page: https://zaena.github.io/portfolio-nadine/
GitHub repo: https://github.com/Zaena/portfolio-nadine

  • Here's some more information that might help with the ordering problem: http://stackoverflow.com/questions/26196559/jekyll-post-order – Bilal Akil Jan 15 '17 at 12:53

2 Answers2

5

The website has several issues:

The posts folder structure is wrong

Posts should always be in a _posts folder, they can't be inside another folder (Jekyll can display them but some functionality is lost, they are not meant to be there)

If categories are folders, they can't start with an underscore (_)

The actual structure has categories defined as /_verslagen, they should be /verslagen and its posts inside /verslagen/_posts

Having the same post in two different folders has unexpected behaviour

There are posts located in different folders, only one version should exist.

Solution

The structure you are looking for is:

├── index.html
├── lesplannen
│   └── _posts
│       ├── 2016-12-10-dit-is-een-lesplan2.md
│       ├── 2016-12-10-dit-is-een-lesplan3.md
│       ├── 2016-12-10-dit-is-een-lesplan4.md
│       └── 2016-12-10-dit-is-een-lesplan.md
└── verslagen
    └── _posts
        ├── 2016-12-10-dit-is-een-verslag10.md
        ├── 2016-12-10-dit-is-een-verslag11.md
        ├── 2016-12-10-dit-is-een-verslag12.md
        ├── 2016-12-10-dit-is-een-verslag2.md
        ├── 2016-12-10-dit-is-een-verslag3.md
        ├── 2016-12-10-dit-is-een-verslag4.md
        ├── 2016-12-10-dit-is-een-verslag5.md
        ├── 2016-12-10-dit-is-een-verslag6.md
        ├── 2016-12-10-dit-is-een-verslag7.md
        ├── 2016-12-10-dit-is-een-verslag8.md
        ├── 2016-12-10-dit-is-een-verslag9.md
        └── 2016-12-10-dit-is-een-verslag.md

This will make posts available under each category in site.categories.verslagen and site.categories.lesplannen.

Then the code to show each category posts looks like:

{% for post in site.categories.verslagen %}
<p>{{post.title}}</p>
<p>{{ post.date | date: "%b %d, %Y" }}</p>
<p>{{post.thumbnail}}</p>
{% endfor %}

{% for post in site.categories.lesplannen %}
<p>{{post.title}}</p>
<p>{{ post.date | date: "%b %d, %Y" }}</p>
<p>{{post.thumbnail}}</p>
{% endfor %}
marcanuy
  • 23,118
  • 9
  • 64
  • 113
  • I get what you're saying! I will implement this and get back to you. Thank you for your detailed comment. – Max de Ruiter Jan 15 '17 at 14:10
  • This worked! Both sections are displaying their respective content. Thanks for taking the time to clarify this, I understand Jekyll a bit better now :) – Max de Ruiter Jan 15 '17 at 14:16
  • Just curious @marcanuy, can you find any documentation suggesting to avoid subdirectories in `_posts`, and that having the same post in two different directories has unexpected behaviour? I've tried but I can't even find the documentation for categories :P – Bilal Akil Jan 15 '17 at 14:29
  • @BilalAkil `_posts` is meant for posts only, subdirectories should be directly under other categories: `category\subcategory1\subcategory2\_posts` (not `category\_posts\subcategory1`). – marcanuy Jan 15 '17 at 14:44
  • Yep, that makes sense now that you've demonstrated it, but I find it strange that this feature doesn't appear to be documented. It's not mentioned in the Directory Structure/Writing Posts/Collections documentation... not sure where else to look - I just can't find it. I had no idea it actually existed until you mentioned it; I thought categories only came from YAML front matter only. All good though, thanks for teaching me about it :D – Bilal Akil Jan 15 '17 at 15:17
  • 1
    @BilalAkil look at the description of page.categories in http://jekyllrb.com/docs/variables/#page-variables. "The list of categories to which this post belongs. Categories are derived from the directory structure above the _posts directory. For example, a post at /work/code/_posts/2008-12-24-closures.md would have this field set to ['work', 'code']. These can also be specified in the YAML Front Matter." – marcanuy Jan 15 '17 at 17:03
  • 1
    Oh there it is! Nice find, and thanks for pointing that out :) – Bilal Akil Jan 16 '17 at 01:17
0

Here's how I got them displaying separately:

index.html

---
---

<p>Posts in dir-a:</p>

<ul>
    {% for post in site.posts %}
        {% assign first_dir = post.path | remove_first: "_posts/" | split: "/" | first %}
        {% if first_dir == 'dir-a' %}
            <li>
                <a href="{{ post.url }}">{{ post.title }}: {{ post.date | date: "%b %d, %Y" }}</a>
            </li>
        {% endif %}
    {% endfor %}
</ul>

<p>Posts in dir-b:</p>

<ul>
    {% for post in site.posts %}
        {% assign first_dir = post.path | remove_first: "_posts/" | split: "/" | first %}
        {% if first_dir == 'dir-b' %}
            <li>
                <a href="{{ post.url }}">{{ post.title }}: {{ post.date | date: "%b %d, %Y" }}</a>
            </li>
        {% endif %}
    {% endfor %}
</ul>

This assumes the following directory structure:

/_posts
/_posts/dir-a
/_posts/dir-b

Should be nice and easy to adapt to your needs.

Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
  • Hi Bilal, thanks so much for your reply. Unfortunately, my page insists on displaying posts 6-9 from dir_a despite specifying dir_b. For simplicity's sake I've uploaded a screenshot of the code: [Screenshot](http://i.imgur.com/3IG2aCr.jpg). Can you spot any errors I've made? Thanks again for your time. – Max de Ruiter Jan 15 '17 at 13:20
  • Ok I can't see anything obvious from the screenshot, so I'm not sure why it's behaving that way. Quick sanity check: did you rebuild the site since changing the code? If you have, then could you push your site to a development branch on your GitHub repo so I can check it out and try and replicate the issue? Also, what version of Jekyll are you using (`jekyll --version`)? – Bilal Akil Jan 15 '17 at 13:59
  • After some trial-and-error, I got the indentation right (which was important since I'm using Jade). However, the articles from dir_a now show up fine, but the ones from dir_b don't show at all. The code is identical. Do you have any insight? – Max de Ruiter Jan 15 '17 at 14:00
  • My current build is now on GitHub! My Jekyll version is 3.3.1. The code can be found in _jadefiles/sections-verslagen and _jadefiles/secions-lesplannen respectively. The former is the one that is currently working. – Max de Ruiter Jan 15 '17 at 14:02
  • That's the same version as me, so it should work at some point :P I'll look into it, but in the meantime here are some debugging tips: you can output the variables after assigning them like so: `{{ first_dir }}`, or check out the intermediate steps: `{{ post.path | remove_first: "_post" }}`. Keep messing around with this and you may realise what's going wrong. – Bilal Akil Jan 15 '17 at 14:04
  • Hey Bilal, the main issue was with my folder structure as Marcanuy pointed out in the other comment. Regardless, cheers for helping out! – Max de Ruiter Jan 15 '17 at 14:19
  • Glad you've got it working :) I actually think the breaking point at the end of our chain was that the files in the `lesplannen` directory used the .MARKUP extension. "MARKUP" was written in the documentation as a placeholder to be replaced depending on which markup you're using (i.e. .html, .md, .etc..). That being said, all of @marcanuy's suggestions would help you have a better and more maintainable project overall. – Bilal Akil Jan 15 '17 at 14:22