3

これは簡単なことのように思えますが、調査して数時間ぶらぶらした後でも、まだ理解できません。私の目標は、Shopify コレクション グリッドに非製品 HTML ブロックを挿入することです。たとえば、行 1、列 3 の「無料の陸送をお楽しみください」ブロックのように:

http://www.katespade.com/designer-handbags/handbags,default,sc.html

私のコレクション グリッドは、ページごとに 3 列 x 4 行に設定されています。すべてのコレクション ページの行 2、列 1 のブロックを 3 つ以上の製品に置き換えたいと考えています。

変更する必要がある Liquid ループは次のとおりです。

    <ul class="product-grid clearfix">
    {% for product in collection.products %}
      <li{% cycle '', '', ' class="last-in-row"' %}>
        {% include 'product-grid-item' %}
      </li>
    {% endfor %}
    </ul>

誰にも洞察がありますか?

4

1 に答える 1

2

スタック オーバーフローへようこそ! :-)

まず、これを疑似コード化して、同じページにいること、およびロジックが正しいことを確認します。

for each product in collection
{
    is this the 4th iteration of the loop?
    (in other words, is it the first item in the second row)
    {
        Add an <li> for the custom non-product block.
    }

    Add an <li> for the standard product block
}

そのロジックがあなたが探しているものに適合する場合、これが Liquid の本物です。

<ul class="product-grid clearfix">

{% for product in collection.products %}

    {% if forloop.index == 4 %}
        <li{% cycle '', '', ' class="last-in-row"' %}>
            {% include 'your-custom-block-element' %}
        </li>
    {% endif %}

    <li{% cycle '', '', ' class="last-in-row"' %}>
        {% include 'product-grid-item' %}
    </li>

{% endfor %}

</ul>

お気づきかもしれませんが、Shopify はデフォルトで 1 ベースのインデックスを扱っています。を探しているのはそのためですforloop.index == 4。他のほとんどすべての言語では、ゼロから始まるインデックスを扱い、 if をチェックしますforloop.index == 3

この慣例に悩まされている場合は、常に を使用forloop.index0して、代わりに基数がゼロのループのインデックスを確認できます。;-)

これでうまくいくかどうか教えてください。幸運を!

于 2013-01-12T01:57:51.877 に答える