Jekyll帖子特定于自定义前端内容

时间:2015-05-28 10:16:40

标签: tags yaml jekyll liquid yaml-front-matter

我花了最后几个小时搜索stackoverflow的深度以及其他令人惊叹的Jekyll教程网站,并且还没有找到解决这个特定问题的方法。 = [

我没有使用site.tags或site.categories,而是创建了自己的自定义标签,名为"子类别"在类别'博客'。目标是尝试对每个进行后计数。我发现的教程完全适用于两个类别AND标签,而不是自定义前端问题。

我的一些子类别是这样写的:

[design]
[gaming]
[design, gaming]

我正在寻找一个将帖子数增加1的代码,因为它意识到包含子类别的帖子。由于我没有使用插件,因此完整的子类别列表实际上是在数据.yml文件中单独列出的(除了我的帖子的开头)。

以下是我写这篇很多可怜的尝试之一:

<ul class="blog__sidebar__subcategories m-t-s">
  {% for subcategory in site.data.subcategories %}

      <a class="blog__sidebar__subcategories__item" href="{{ site.baseurl }}/blog/{{ subcategory.class }}">
      <li>{{ subcategory.class }}

          {% assign counter = '0' %}
          {% assign subcat_data == site.data.subcategories %}
            {% for post in site.categories.blog  %}
            {% if subcat_data == post.subcategories %}
              {% capture counter %}{{ counter | plus: '1' }}{% endcapture %}
            {% endif %}
            {% endfor %}
            ({{ counter }})
      </li>
      </a>
  {% endfor %}
  </ul>

我发现的问题包括输出具有不断的重复,或吐出&#34;设计,游戏&#34;作为一个实体。这导致如下:

design | 6 
gamingdesign | 6 
gaming | 6 
gaming | 6

这是我的.yml文件的外观(简单):

- class: design

- class: gaming

我的代码在尝试添加帖子计数之前(有效!):

<ul class="blog__sidebar__subcategories m-t-s">
  {% for subcategory in site.data.subcategories %}


      <a class="blog__sidebar__subcategories__item" href="{{ site.baseurl }}/blog/{{ subcategory.class }}">
      <li>{{ subcategory.class }}</li>
      </a>
  {% endfor %}
  </ul>

如果我不小心违反了stackoverflow的任何社交礼仪,请告诉我。第一次发帖!万分感谢你。

1 个答案:

答案 0 :(得分:0)

Jekyll在用户阵列方面表现不佳。我认为你最好坚持Jekyll现有的机制。

因此,您可以将标记用于子类别。

---
layout: post
date: 2014-08-14 07:51:24 +02:00
title: Your title
categories: [ blog ]
tags:
  - design
  - gaming
---

你的循环可以是

{% for tag in site.tags %}

  {% comment %}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    tag = Array [
            "design",
            Array [
              #Jekyll:Post @id="/blog/1993/02/08/index",
              #Jekyll:Post @id="/blog/1991/08/14/index"
            ]
          ]

    Values in this tag array are :
      - tag[0] -> "design"
      - tag[1] -> an Array of posts that have the design tag

    Here, we can already do a  "{{ tag [0] }} : {{ tag[1] | size }}" 
    that will give us the count for each tag's posts array.

    But if one post insn't in "blog" category but has a tag used 
    in the "blog" category, it will be counted here.

    Where must count only posts that are in the "blog" category.
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++{% endcomment %}

  {% assign counter = 0 %}

  {% for post in tag[1]  %}
    {% if post.categories contains "blog" %}
      {% assign counter = counter | plus: 1 %}
    {% endif %}
  {% endfor %}

  {% comment %}+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  Only print counter if the current tag has "blog" posts
  +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++{% endcomment %}
  {% if counter > 0 %}
      <p>{{ tag[0] }} : {{ counter }}</p>
  {% endif %}

{% endfor %}