jekyll / jekyll-archives

:books: Archive pages for your Jekyll tags and categories.
https://jekyll.github.io/jekyll-archives/
MIT License
438 stars 99 forks source link

Loop through site.posts? #134

Closed chrisfinazzo closed 5 years ago

chrisfinazzo commented 5 years ago

For the examples listed on the layouts page, shouldn't the loop be {% for post in page.posts %}? As written, when I try this on my own site nothing renders, but if I use site.posts an archive appears.

Jekyll 3.8.5 Jekyll Archives 2.2.1 Ruby 2.5.0

ashmaroli commented 5 years ago

From the source-code, it looks like page.posts is the correct accessor: https://github.com/jekyll/jekyll-archives/blob/44e357429136e8b70d31537814d1532f107b8932/lib/jekyll-archives/archive.rb#L26-L29

Do you have a sample test-repository that I can clone and reproduce this..?

chrisfinazzo commented 5 years ago

I don't have a testing repo, can reproduce my setup here. While this currently works, I'm wondering if there's a simpler way to go about it. For the record, page.posts is correct, my bad.

    jekyll-archives:
      enabled: year
      layout: archive
      permalinks:
        year: '/archive/:year/'
---
layout: default
---

{% for post in site.posts  %}
        {% capture this_year %}{{ post.date | date: "%Y" }}{% endcapture %}
        {% capture next_year %}{{ post.previous.date | date: "%Y" }}{% endcapture %}

    {% if forloop.first %}
    <h2 id="{{ this_year }}-ref">{{this_year}}</h2>
    <ul>
    {% endif %}

    <li><a href="{{ post.url }}">{{ post.title }}</a></li>

    {% if forloop.last %}
    </ul>
    {% else %}
        {% if this_year != next_year %}
        </ul>
        <h2 id="{{ next_year }}-ref">{{next_year}}</h2>
        <ul>
        {% endif %}
    {% endif %}
{% endfor %}
ashmaroli commented 5 years ago

What is your use-case @chrisfinazzo ? The above Liquid code is quite a nasty hack! It considerably lowers you site's maintainability score.

I'm closing this since this does not look like a bug-report to me. You may find assistance at Jekyll Talk..