简体   繁体   中英

Jekyll: can't we loop in the posts (and display the list) from each post?

I want to display a list of posts in a category page: this loop works. But I also want to display this list on each of these posts: it's the same loop but it doesn't work.

There is my Jekyll loop:

{% for post in site.categories.[include.nomrecueil] reversed %}
  <li>{{ post.title }</li>
{% endfor %}

Short version

My loop is not fetching posts depending on the layout used. I have the following structure:

categoryA
    | index.md
    | categoryB
        | index.md
        | _posts
            | article.md

I have two layouts that include the same loop. My loop in categoryB/index.md is working, but the same in article.md doesn't work. Any idea why? Can't we loop in the posts (and display the list) from each post?

Details

You may need more information, so you can take a look at the repo if you need. The loop is at line 35 of the include nav-poemes.html which is included in two layouts: a) recueil.html and b) poem.html . The loop outputs correctly in the index.md that calls the recueil.html layout, but not in the article.md that calls the poem.html layout.

I checked that the include parameter nomrecueil that's in the loop is well passed through both layouts. No posts are fetched in the second layout with the same loop... Am I using Jekyll the wrong way or is it a bug?

I do not know why... but this works (which must have something to do with the 'contains' statement):

{% assing includenomrecueil = include.nomrecueil %}
<ul>
  {% for category in site.categories %}
    {% assign catfirst = category | first %}
    {% if includenomrecueil contains catfirst %}
      {% for posts in category %}
        {% for p in posts reversed %}
          <li>...</li>
        {% endfor %}
      {% endfor %}
    {% endif %}
  {% endfor %}
</ul>

In your code the variable comes through. I even tried to add spaces to the variable and remove them (to make sure it is a string). Remarkable is that this works:

{% for post in site.categories['fleurs-persistantes'] reversed %}
  <li>...</li>
{% endfor %}

Even when {{ include.nomrecueil }} outputs fleurs-persistantes , while at the same time this does not work:

{% for post in site.categories[include.nomrecueil] reversed %}
  <li>...</li>
{% endfor %}

Really strange! I do not understand it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM