首页 > 解决方案 > 突出显示 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。

我不知道我在这里错过了什么。

标签: shopify

解决方案


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

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

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

推荐阅读