在shopify中突出显示活动的收藏按钮

问题描述

我有一个要在Shopify中调用的集合的列表。我尝试使用link active标记,但无法正常工作。这是我的片段

{% for collection in collections %}
  {% unless collection.handle == 'frontpage' %}
    <button type="button" class="btn btn-outline-info {% if link.active %}class='active'{% endif %}">{{ collection.title | escape | link_to: collection.url }}</button>
  {% endunless %}
{% endfor %}

我正在尝试将active class添加到活动集合或当前使用的集合URL。

我不知道我在这里想念什么。

解决方法

根据您的注释,您的代码将无法正常运行。 collection是Shopify中的保留变量,通过在循环中使用相同的变量,您可能会完全更改实际的集合。其次,link.active将仅在linklists循环内工作。

您可以执行以下操作:更改循环中单元的分配变量,并验证句柄是否相同。

{% for thisCollection in collections %}
  {% unless thisCollection.handle == 'frontpage' %}
    <button type="button" class="btn btn-outline-info {% if thisCollection.handle == collection.handle %}class='active'{% endif %}">{{ thisCollection.title | escape | link_to: thisCollection.url }}</button>
  {% endunless %}
{% endfor %}