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

后端 未结 4 1112
逝去的感伤
逝去的感伤 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:30

    There's a cleaner way to do this using the liquid "contains" property:

    In _config.yml, add your index of categories

    categories: [fruit, meat, vegetable, cheese, drink]
    

    In your page.md inside the front matter, add one or more of the categories available in the _config.yml

    ---
    layout: page
    title: Orange juice
    description: Orange juice is juice from oranges. It's made by squeezing oranges.
    categories: [fruit, drink]
    ---
    

    In your template to get all the pages in the fruit category you do:

    {% for page in site.pages %}
      {% if page.categories contains 'fruit' %}
        

    {{page.title}}

    {{page.description}}

    {% endif %} {% endfor %}

提交回复
热议问题