3
votes

I'm trying to do the following:

{% for post in site.categories.{{ post.designer }} %}

So that when placing the above code inside a single post, it can display a list of posts from the current post's category.

However I don't think it's working because it just keeps returning undefined. My question is, is it possible in Jekyll or Liquid to place variables inside logic expressions?

Thanks

1

1 Answers

1
votes

I suppose that "designer" is the category of your post?
If yes, you can't get it via post.designer.

You need to use page.categories instead (according to Page variables).

A post can have more than one category, so you can't just place page.categories in your loop because it's an array.

There are two possible solutions:

  1. Loop through all the categories of the post, and then do your loop for each category:

    {% for cat in page.categories %}
      <h1>{{ cat }}</h1>
      <ul>
        {% for post in site.categories[cat] %}
          <li><a href="{{ post.url }}">{{ post.title }}</a></li>
        {% endfor %}
      </ul>
    {% endfor %}
    
  2. If your post has only one category, you can omit the outer loop from my first example and just use the first element of the page.categories array:

    <ul>
      {% for post in site.categories[page.categories.first] %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    </ul>
    

    or

    {% assign firstcat = page.categories | first %}
    
    <ul>
      {% for post in site.categories[firstcat] %}
        <li><a href="{{ post.url }}">{{ post.title }}</a></li>
      {% endfor %}
    </ul>