计算 jekyll 模式面包屑列表中的循环值

Counting loop value in jekyll schema breadcrumb list

这个问题与What are some good ways to implement breadcrumbs on a Jekyll site?相关,我需要从循环中获取位置。 <meta itemprop="position" content="">

<nav class="breadcrumbs bootstrap hidden-sm-down">
<ol class="breadcrumb list-unstyled" itemscope itemtype="https://schema.org/BreadcrumbList">
 <li class="breadcrumb-item" itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><a itemprop="item" href="/"><span itemprop="name">Home</span></a><meta itemprop="position" content="1"></li>
 {% assign crumbs = page.url | split: '/' %}
 {% assign crumbs_total = crumbs | size | minus: 1 %}
   {% for crumb in crumbs offset: 1 %}
    {% if forloop.index == crumbs_total %}
        <li class="breadcrumb-item active"itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><meta itemprop="item" content="{{ site.url }}{{ page.url | relative_url }}"><span itemprop="name">{{ page.crumbtitle }}</span><meta itemprop="position" content=""></li>
    {% else %}
        <li class="breadcrumb-item" itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><a itemprop="item" href="{% assign crumb_limit = forloop.index | plus: 1 %}{% for crumb in crumbs limit: crumb_limit %}{{ crumb | append: '/' }}{% endfor %}"><span itemprop="name">{{ crumb | replace:'-',' ' | upcase }}</span></a><meta itemprop="position" content=""></li>
    {% endif %}
  {% endfor %}
</ol>
</nav>

第 12 行 只需添加一个。 <meta itemprop="position" content="{{ crumbs_total | plus:1 }}"> 第 16 行 已经有变量,{% assign crumb_limit = forloop.index | plus: 1 %} 所以,在 行 # 18 你只需要 <meta itemprop="position" content="{{ crumb_limit }}">

{% if page.layout != 'home' %}
<nav class="breadcrumbs bootstrap hidden-sm-down">
<ol class="breadcrumb list-unstyled" itemscope itemtype="https://schema.org/BreadcrumbList">
 <li class="breadcrumb-item" itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><a itemprop="item" href="/"><span itemprop="name">Home</span></a><meta itemprop="position" content="1"></li>
 {% assign crumbs = page.url | split: '/' %}
 {% assign crumbs_total = crumbs | size | minus: 1 %}
    {% for crumb in crumbs offset: 1 %}
    {% if forloop.index == crumbs_total %}
        <li class="breadcrumb-item active"itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><meta itemprop="item" content="{{ site.url }}{{ page.url | relative_url }}"><span itemprop="name">{{ page.crumbtitle }}</span><meta itemprop="position" content="{{ crumbs_total | plus:1 }}"></li>
    {% else %}
{% assign crumbs_position = 1 %}
        <li class="breadcrumb-item" itemprop="itemListElement" itemscope
      itemtype="http://schema.org/ListItem"><a itemprop="item" href="{% assign crumb_limit = forloop.index | plus: 1 %}{% for crumb in crumbs limit: crumb_limit %}{{ crumb | append: '/' }}{% endfor %}"><span itemprop="name">{{ crumb | replace:'-',' ' | upcase }}</span></a>{% assign crumbs_postion = crumbs_position | plus:1 %}<meta itemprop="position" content="{{ crumb_limit }}"></li>
    {% endif %}
  {% endfor %}
</ol>
</nav>
{% endif %}