【问题标题】:Jekyll post counts specific to custom front matterJekyll 帖子计数特定于自定义前端问题
【发布时间】:2015-08-10 18:57:08
【问题描述】:

在过去的几个小时里,我一直在深入研究 stackoverflow 以及其他令人惊叹的 Jekyll 教程网站,但还没有找到解决这个特定问题的方法。 =[

我没有使用 site.tags 或 site.categories,而是在“博客”类别下创建了自己的名为“子类别”的自定义标签。目标是尝试为每个人获得一个后期计数。我发现的教程完美地适用于类别和标签,只是不适用于自定义前端。

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

[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>

我发现的问题包括输出不断重复,或者将“设计、游戏”作为一个实体吐出。这会导致如下结果:

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 的任何社交礼仪,请告诉我。第一次发帖!谢谢你一百万。

【问题讨论】:

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


    【解决方案1】:

    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 %}
    

    【讨论】:

    • 您好! Yowza,效果很好。 This 使用前端过滤的插件也引起了我的注意,它可以工作(尽管我不喜欢使用插件)。插件和您的解决方案都工作得很好!非常感谢你的帮助。我想我会在此期间交叉手指让用户数组在 Jekyll 中玩得更好?
    猜你喜欢
    • 2015-12-08
    • 2015-09-05
    • 2021-12-25
    • 1970-01-01
    • 2016-08-07
    • 2021-07-11
    • 2018-05-21
    • 2021-12-26
    • 2017-09-11
    相关资源
    最近更新 更多