# 特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい

**URL:** <https://community.shopify.com/t/topic/5292>\
**Category:** 技術的なQ＆A\
**Created:** [August 16, 2019, 8:52am UTC](https://community.shopify.com/t/topic/5292 "2019-08-16T08:52:02Z")\
**Posts on this page:** 8\
**Page:** 1

## AI-generated summary

Shopifyのコレクション一覧で、特定のコレクションテンプレートを使っているものだけを表示する方法が主題です。Liquidの`collection.template_suffix`（テンプレート名の接尾辞）を条件に使うことで、`collection.test.liquid`のような独自テンプレート利用コレクションだけを絞り込めることが確認されました。

実際に動作した例として、`for collection in collections`のループ内で`collection.template_suffix == 'coordinate'`を判定する方法が共有され、当初の目的である一覧表示は解決しています。

その後の論点はページネーションです。`paginate collections by 10`のままでは、表示対象外のコレクションも総件数に含まれてしまう問題がありました。

`where`フィルタで事前に絞り込んでから`paginate`したいという試行はうまく動かず、途中では「Liquidでは現状対応が難しく、AjaxやJavaScript APIが必要そう」との見解が示されました。一方、最後に`assign mod_collection = collections | where: "template_suffix", "coordinate"`としてからページネーションへ渡す案も追加されており、この点は未検証で結論は保留です。

<div class="post-metadata">

**Author:** ![diamondo](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@diamondo](https://community.shopify.com/u/diamondo)\
**Post date:** [August 16, 2019, 8:52am UTC](https://community.shopify.com/t/topic/5292/1 "2019-08-16T08:52:02Z")

</div>

■前提・実現したいこと

ＴＯＰページやコレクションリストページで、特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい

例）  
**コレクションＡ**

（使用テンプレート：collection.liquid → デフォルトのコレクションテンプレ）  
**コレクションＢ**

（使用テンプレート：collection.test.liquid → 独自で作成したコレクションテンプレ）  
**コレクションＣ**

（使用テンプレート：collection.test.liquid）

→ Ｂ・Ｃのコレクションのみ表示したい

■この機能を実装しているshopifyサイト

[https://store.rili.tokyo/collections](https://store.rili.tokyo/collections)  
コレクションリストページに「コーデから探す」系のコレクション一覧のみ表示。  
（その他、トップスやアウターなどのコレクションは表示させていない）

■試したこと

```auto
{% for collection in collections %}
{{ collection.title }}
{% endfor %}

```

このコレクション一覧表示のコードをベースに、  
「特定のコレクションテンプレートを使用している」という条件を追加するために色々試したのですがうまくいきませんでした。

---

<div class="post-metadata">

**Author:** ![junichiokamura](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/junichiokamura/32/299737_2.png) [@junichiokamura](https://community.shopify.com/u/junichiokamura)\
**Post date:** [August 20, 2019, 3:10am UTC](https://community.shopify.com/t/topic/5292/2 "2019-08-20T03:10:40Z")

</div>

diamondoさん、こんにちは。

以下ののcollection idでフィルタするか、

[https://help.shopify.com/en/themes/liquid/objects/collection](https://help.shopify.com/en/themes/liquid/objects/collection)

使っているテンプレートで判別する場合は以下のtemplate-suffixで判断するのはいかがでしょうか？

[https://help.shopify.com/en/themes/liquid/objects/collection#collection-template\_suffix](https://help.shopify.com/en/themes/liquid/objects/collection#collection-template_suffix)

---

<div class="post-metadata">

**Author:** ![diamondo](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@diamondo](https://community.shopify.com/u/diamondo)\
**Post date:** [August 28, 2019, 7:29am UTC](https://community.shopify.com/t/topic/5292/3 "2019-08-28T07:29:52Z")

</div>

ありがとうございます！

ご教示いただきました「template-suffix」で下記コードを試したところ、コレクションテンプレートを条件にループを回すことができました。

```auto
{% for collection in collections %}
　{% if collection.template_suffix == 'coordinate' %}
　　ここに処理内容
　{% endif %}
{% endfor %}

```

また、立て続けのご質問で大変恐縮なのですが、ページネーションの該当条件にもコレクションテンプレートを適用することは可能でしょうか？

下記のようなコードですと特定のコレクションテンプレート以外のコレクションもページネーションの数に含まれてしまいます。

```auto
{% paginate collections by 10 %}

{% for collection in collections %}
 　{% if collection.template_suffix == 'coordinate' %}
　　ここに処理内容
 　{% endif %}
 {% endfor %}

{% if paginate.pages > 1 %}
　　{{ paginate | default_pagination }}
{% endif %}

{% endpaginate %} 

```

---

<div class="post-metadata">

**Author:** ![junichiokamura](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/junichiokamura/32/299737_2.png) [@junichiokamura](https://community.shopify.com/u/junichiokamura)\
**Post date:** [August 29, 2019, 3:48am UTC](https://community.shopify.com/t/topic/5292/4 "2019-08-29T03:48:36Z")

</div>

検証はしていませんが、Filter whereを paginate collections | where: “template\_suffix”, “coordinate” のように使えませんでしょうか？

[https://help.shopify.com/en/themes/liquid/filters/array-filters#where](https://help.shopify.com/en/themes/liquid/filters/array-filters#where)

---

<div class="post-metadata">

**Author:** ![diamondo](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@diamondo](https://community.shopify.com/u/diamondo)\
**Post date:** [August 31, 2019, 3:38pm UTC](https://community.shopify.com/t/topic/5292/5 "2019-08-31T15:38:01Z")

</div>

Filter whereを使って下記のように色々試したのですが、動作しませんでした。

```auto
{% paginate collections | where: "template_suffix", "coordinate" %}

```

```auto
{% paginate collections | where: "template_suffix", "coordinate" by 10 %}

```

```auto
{% assign aaaa = paginate collections | where: "template_suffix", "coordinate" %} 
{% paginate aaaa by 10 %}
⇒エラー表示

```

---

<div class="post-metadata">

**Author:** ![junichiokamura](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/junichiokamura/32/299737_2.png) [@junichiokamura](https://community.shopify.com/u/junichiokamura)\
**Post date:** [September 2, 2019, 2:37am UTC](https://community.shopify.com/t/topic/5292/6 "2019-09-02T02:37:38Z")

</div>

検証していただきありがとうございます。

英語ページでも調べてみましたが、現状liquidによる対応はできないようです。

[https://community.shopify.com/c/Shopify-Design/Paginate-only-available-products/td-p/25762](https://community.shopify.com/c/Shopify-Design/Paginate-only-available-products/td-p/25762)

AjaxによるJavaScript APIで取得するなど他の方法が必要になりそうです。

---

<div class="post-metadata">

**Author:** ![diamondo](https://avatars.discourse-cdn.com/v4/letter/d/ce73a5/32.png) [@diamondo](https://community.shopify.com/u/diamondo)\
**Post date:** [September 2, 2019, 12:11pm UTC](https://community.shopify.com/t/topic/5292/7 "2019-09-02T12:11:04Z")

</div>

liquidによる対応はできないのですね。

色々とご教示いただきまして、ありがとうございます！

「特定のコレクションテンプレートを使用しているコレクションのみ一覧表示したい」という機能が実装でき、非常に助かりました。

---

<div class="post-metadata">

**Author:** ![WEBUILD](https://yyz1.discourse-cdn.com/shopifyforum/user_avatar/community.shopify.com/webuild/32/271922_2.png) [@WEBUILD](https://community.shopify.com/u/WEBUILD)\
**Post date:** [November 13, 2019, 2:22am UTC](https://community.shopify.com/t/topic/5292/8 "2019-11-13T02:22:35Z")

</div>

一度 assign でオブジェクトを定義して それから pagination に渡してはどうでしょうか？

```auto
{% assign mod_collection = collections | where: "template_suffix", "coordinate" %}
{% pagination mod_collection by 10 %}

```

[diamondo](https://community.shopify.com/user/595258)さんがお試しになられた3番目に近いと思いますが、

paginationを 含めた assignはできないのでエラーになっているのだと思います。  
試してないので推測ですが。。。
