Creating categories on Jekyll driven site

前端 未结 4 1297
野趣味
野趣味 2021-02-01 07:41

I\'m having a hard time understanding how to generate archive pages for each category I use on my blog. I\'d like the user to be able to click on a category and then be taken to

4条回答
  •  星月不相逢
    2021-02-01 07:53

    Note: I'm linking examples here that are using tags (because the examples already existed, with tags), but they work the same for categories.


    If you don't want to use a plugin, for example if you want your site to work on GitHub Pages, you have only two options:

    1. Create a single page which contains all categories, alphabetically sorted

    2. Indeed create a separate HTML file for each category manually, but put as much as possible into a layout file, so creating a new category page isn't much work:

      /_layouts/tagpage.html:

      ---
      layout: default
      ---
      
      

      {{ page.tag }}

        {% for post in site.tags[page.tag] %}
      • {{ post.date | date: "%B %d, %Y" }}: {{ post.title }}
      • {% endfor %}

      With this layout file, you need only two lines of YAML front-matter to add a new tag page:
      (in this case for the jekyll tag)

      /tags/jekyll/index.html:

      ---
      layout: tagpage
      tag: jekyll
      ---
      

      So the actual effort to create a new tag page is minimal - the only thing is that you need to remember to do it when you're using a new tag for the first time.

提交回复
热议问题