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

时间:2019-06-29 21:03:43

标签: jekyll liquid jekyll-theme

这个问题与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>

1 个答案:

答案 0 :(得分:0)

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