Jekyll blog show posts under a category

前端 未结 3 1548
孤城傲影
孤城傲影 2021-01-03 04:08

I want to show posts that are from a certain category. For example, going to url http://example.com/posts/programming will list all the posts that have \"programming\" as th

3条回答
  •  独厮守ぢ
    2021-01-03 04:40

    You could use a plugin (for that, see David Jacquel's answer)...but you can't use it if you want to host your site on GitHub Pages, because GitHub Pages supports only a few plugins, and jekyll-archives isn't one of them.

    So if you can't use a plugin, AFAIK there's no other way than to create a page for each category manually.
    I'm no JavaScript guru, but I'm quite sure that it's not possible to dynamically change the category with JavaScript, because Jekyll pages are compiled once and can't be changed on the fly at runtime.

    But creating a new category page for each category isn't as much work as it seems.

    I have a blog post here where I explain how to do it:
    Separate pages per tag/category with Jekyll (without plugins)

    Short version:
    (I'm using tags instead of categories here, but both work exactly the same)

    1. Create a special layout file /_layouts/tagpage.html:

      ---
      layout: default
      ---
      
      

      {{ page.tag }}

        {% for post in site.tags[page.tag] %}
      • {{ post.date | date: "%B %d, %Y" }}: {{ post.title }}
      • {% endfor %}
    2. With this layout file, you need only two lines of YAML front-matter to create a new tag page, for example /tags/jekyll/index.html:

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

提交回复
热议问题