Generating a list of pages (not posts) in a given category

后端 未结 4 1100
逝去的感伤
逝去的感伤 2020-12-22 16:42

I am using Jekyll as a static generator for a website (not a blog), and I want to have an automatically generated list of all pages on my index page. Specifically, I want to

4条回答
  •  春和景丽
    2020-12-22 17:20

    There are some variations/ simplifications possible (answer of felipesk). Maybe due to improvements in Jekyll.

    There is NO index needed in _config.yml.

    If the list of pages are not listed in a page but for example in a doc, you can add the category also to the doc:

    ---
    layout: doc
    title: Fruit List
    categories: [fruit]
    ---
    

    And then use it like this:

    {% for p in site.pages %}
       {% if p.categories contains page.category %}
         * [{{ p.title }}]({{ p.url | absolute_url }})
            {{ p.excerpt }}
       {% endif %}
    {% endfor %}
    

    With posts this can even be shorter:

    {% for post in site.categories[page.category] %}
      * [{{ post.title }}]({{ post.url | absolute_url }})  
          {{ post.excerpt }}
    {% endfor %}
    

    Why this only works for posts, I could not figure out yet.

    The interesting point is that this snippet can be used everywhere (if you mix docs/pages/posts)! So just add it as an _includes and use it like:

    ## Further Reading
    {% include pages-list.md %}
    

    I work with the theme Minimal Mistakes

提交回复
热议问题