如何使 Shopify 集合按选项排序显示所选值?加载后返回默认 'Sort By'

How to make the Shopify collections sort by option show the selected value? It goes back to the default 'Sort By' after loading

当用户点击某个选项时,例如价格,从低到高 - 页面刷新,但 select 下拉菜单返回默认值,显示 "Sort by" 而不是显示价格,从低到高。有没有办法让它显示 价格,从低到高 目前是 selected?

目前我的代码是:

<div class="collection-sorting styled-select mr3-l mb2-l">
  {%- assign selected = collection.sort_by | default: collection.default_sort_by -%}
  <select name="sort_by" onchange="location = this.value;" id="sorting">

    <option value="placeholder" disabled {% if selected == false %}selected{% endif %}>
      {{ 'collection.sort.title' | t }} by
    </option>

    {% for option in collection.sort_options %}
      <option value="?sort_by={{ option.value }}" {% if value == selected %}selected{% endif %}>
        {{ option.name }}
      </option>
    {% endfor %}

    {% comment %} Clear sorting {% endcomment %}
    <option value="{{ collection.url }}">Clear</a>
  </select>
  {% include 'inline-icon' with svg: 'icon-arrow-down.svg', class: 'current-color-stroke' %}
</div>

在您的 select 上方添加以下内容:

{%- assign selected = collection.sort_by | default: collection.default_sort_by -%}