2
votes

I moved from Jekyll pre-1.0 to 2.0 recently.

In my original code, on each blog post it will list all the title of posts that belongs to the same category as the current post being viewed. Previously this code worked:

{% for post in site.categories.[page.category] %}
    <li {% if page.title == post.title %} class="active" {% endif %}>
    <a href="{{ post.url}}">{{ post.title }}</a></li>
{% endfor %}

However in the new version this does not work and I have to specify the category individually like so:

{% for post in site.categories.['NAME_OF_CATEGORY'] %}

Why can't I dynamically check for the category as before? And is there a work around for this instead of using if statements?

1

1 Answers

0
votes

I figured it out. I had, in each post, my YAML front-matter category variables in uppercase or Camel case. Example: category: ABC or category: Zyx.

Doing page.category will always return the the actual category as it was written in the front-matter, which is ABC or Zyx. However site.categories.[CAT] only accepts CAT in lower cases (down case in liquid language).

Hence this will work site.categories.['abc'] or site.categories.['xyz']. But this will fail site.categories.['ABC'] or site.categories.['Xyz']. It is the same as doing site.categories.[page.category].

Solution. Assign the current page category in lower case like so:

{% assign cat = page.category | downcase %}

   {% for post in site.categories.[cat] %}
       <li {% if page.title == post.title %} class="active" {% endif %}>
       <a href="{{ post.url}}">{{ post.title }}</a></li>
   {% endfor %}