简体   繁体   English

在Jekyll驱动的网站上创建类别

[英]Creating categories on Jekyll driven site

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 a page that lists out all articles with the desired category assigned. 我希望用户能够点击某个类别,然后转到一个页面,列出所有分配了所需类别的文章。

The only way I can think of doing it is by manually creating a specific html file for each category in the root. 我能想到的唯一方法是手动为根中的每个类别创建一个特定的html文件。 But I'm sure there must be a more dynamic way? 但我敢肯定必须有一种更有活力的方式吗?

I have the site hosted on github - https://github.com/sirbrad/sirbrad.github.com 我的网站托管在github上 - https://github.com/sirbrad/sirbrad.github.com

Thanks in advance! 提前致谢!

Brad 布拉德

You can generate a list of all the available categories by using the site.categories data, using the first element of each category (which is an array) to get the category name: 您可以使用site.categories数据生成所有可用类别的列表,使用每个类别的第一个元素(这是一个数组)来获取类别名称:

{% for cat in site.categories %}
    <li>{{ cat[0] }}</li>
{% endfor %}

And you can generate a list of all the posts in a given category like so: 并且您可以生成给定类别中所有帖子的列表,如下所示:

{% for post in site.categories.CATEGORY_NAME %}

It doesn't seem possible to generate an individual HTML page for each category as you were hoping, but perhaps a good compromise would be to generate a single page containing a list of all categories, where each category contains all the posts in that category. 似乎不可能像您希望的那样为每个类别生成单独的HTML页面,但也许一个很好的折衷方案是生成包含所有类别列表的单个页面,其中每个类别包含该类别中的所有帖子。 You could then use some simple JavaScript to hide the posts in each category until the category name is selected, giving almost the same user experience as individual archive pages for each category. 然后,您可以使用一些简单的JavaScript来隐藏每个类别中的帖子,直到选择了类别名称,从而为每个类别提供与各个存档页面几乎相同的用户体验。

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: 如果您不想使用插件,例如,如果您希望您的网站使用GitHub页面,则只有两个选项:

  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 : 确实手动为每个类别创建一个单独的HTML文件, 但尽可能多地放入布局文件中,因此创建一个新的类别页面并不是很有用

    /_layouts/tagpage.html : /_layouts/tagpage.html

     --- layout: default --- <h1>{{ page.tag }}</h1> <ul> {% for post in site.tags[page.tag] %} <li> {{ post.date | date: "%B %d, %Y" }}: <a href="{{ post.url }}">{{ post.title }}</a> </li> {% endfor %} </ul> 

    With this layout file, you need only two lines of YAML front-matter to add a new tag page: 使用此布局文件,您只需要两行YAML前端内容即可添加新的标记页:
    (in this case for the tag) (在这种情况下为标签)

    /tags/jekyll/index.html : /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. 因此,创建新标记页面的实际工作量是最小的 - 唯一的一点是,当您第一次使用新标记时,需要记住这样做。

You can use Dave Perett's generate_categories.rb plugin to automatically create a page for each category in your site. 您可以使用Dave Perett的generate_categories.rb插件为您网站中的每个类别自动创建一个页面。 Then, use a for loop to run through your site categories and create a link for each in your navigation (or wherever you want to link to the archive pages), as Jon did in his answer to your quesiton. 然后,使用for循环运行您的站点类别,并为导航中的每个(或者您想要链接到存档页面的任何位置)创建链接,就像Jon在回答您的问题时所做的那样。

For github pages, you can make an archive page with 对于github页面,您可以创建一个存档页面

{% for pt in site.categories %}[{{pt[0]}}](#cat-{{pt[0]}}), {% endfor %}

{% for cat in site.categories %}
{% assign nt = cat[0] %}

#### {{ nt }} {#cat-{{nt}}}
<ul> 
  {% for post in site.posts %}
     {% for pt in post.categories %}
    {% if nt == pt %}
      <li>
        {{post.published}} <a href="{{ post.url }}">{{ post.title }}</a>
      </li>
    {% endif %}  
   {% endfor %} 
  {% endfor %}
</ul>  
{% endfor %}

On my machine, with about 200 posts it takes 3s to generate the whole site. 在我的机器上,大约有200个帖子需要3个才能生成整个站点。 This is because the inner if is executed categories x number_of_posts times. 这是因为内部if执行类别x number_of_posts次。 On the other hand, you will have an archive page without using any plugin. 另一方面,您将拥有一个不使用任何插件的存档页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM