New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Listing all post from category dynamically breaks #2440

Closed
stopar opened this Issue May 24, 2014 · 2 comments

Comments

Projects
None yet
3 participants
@stopar

stopar commented May 24, 2014

I need help. 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?

@yihangho

This comment has been minimized.

Contributor

yihangho commented May 24, 2014

I guess you should be using page.categories instead of page.category:

{% for category in page.categories %}
  {% for post in site.categories[category] %}
    {{ post.title }}
  {% endfor %}
{% endfor %}

You can skip the outer loop and replace category with page.categories[0] if your posts belong to only one category, or if you wish to use only the first category.

@stopar

This comment has been minimized.

stopar commented May 26, 2014

Nope, the page.categories[0] didn't work.

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

@stopar stopar closed this May 26, 2014

@jekyll jekyll locked and limited conversation to collaborators Feb 27, 2017

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.