Post index for posts with several conditionals (categories, language, tags) in Jekyll

StackOverflow https://stackoverflow.com/questions/23153683

  •  05-07-2023
  •  | 
  •  

Question

I'd like to have several post-indexes (indices?) on my Jekyll-powered website for several categories and languages, meaning more than one conditional.

My posts have YAML frontmatter including:

categories:
   - research
   - blog
lang: en

Now I'd like a post index, for, say all research posts in english.

I can do:

{% for post in site.posts %}
    {% if post.lang == 'en' and post.categories contains 'research' %}
        {{ post.title }}
    {% endif %}
{% endfor %}

That, however, screws up such niceties as limits and sort by year etc. For limiting the number of posts (etc.), Jekyll still seems to be looking at the whole list of posts, not just for whom the conditional applies.

Something like this pseudocode would be ideal: {% for post in site.posts where post.lang == 'en' and post.categories contains 'research' %}

Any thoughts?

Ps.: I can't use any plugins; want it to be GH Pages compatible. Pps.: I know I could use language as a category, but that doesn't feel right -- these are different pieces of information.

Was it helpful?

Solution

I believe you can do this:

 {% assign resindex = site.posts | where: "category", "research", | where: "lang", "en" | sort: "title" %}
 {% for post in resindex limit:4 %} 
 <div>
 <p><a href="{{ post.url }}">{{ post.title | truncate: 52 }}</a></p>
  </div> 
 {% endfor %}

Though I am fairly confused by the use of category and categories and when you can/should use them. For my example category worked, but in my front matter I have: category: mycategory and am only using one. But the rest of the code should work, and you can limit and sort.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top