Shopify技术问询:如何在集合模板中显示指定标题的集合
Hey there! Let's sort out how to correctly display a specific collection by its title (or handle) in your collection-template.liquid section—since the old methods you found online are outdated with current Shopify Liquid behavior, or maybe you just had a syntax misstep.
Why Your Previous Attempt Didn't Work
The collection variable in your section refers to the current collection loaded by the parent collection.liquid template (e.g., the collection a visitor clicked on to reach the page). Trying to access collection['Collection Title'] is invalid syntax here—you need to use Shopify's global collections object to fetch a specific collection by its identifier.
Method 1: Hardcode a Specific Collection (By Title or Handle)
This works if you want the section to always display one fixed collection, no matter what page it's loaded on.
- First, assign your target collection using the
collectionsobject. Use the collection's handle for reliability (handles don't change if you edit the collection's title later):
If you must use the title, match it exactly (including spaces and capitalization):{% assign target_collection = collections['your-collection-handle'] %}{% assign target_collection = collections['Summer Sale Collection'] %} - Then loop through the collection's products, with a fallback in case the collection doesn't exist:
{% if target_collection %} <h2>{{ target_collection.title }}</h2> {% for product in target_collection.products %} <!-- Your existing product display markup goes here --> <div class="product-item"> <a href="{{ product.url }}">{{ product.title }}</a> <!-- Add price, image, etc. --> </div> {% endfor %} {% else %} <p>Oops, that collection doesn't exist!</p> {% endif %}
Method 2: Make the Section Configurable (Recommended)
For a more flexible solution (so you can pick the collection via the Shopify Theme Editor instead of hardcoding), add a collection picker to your section's schema:
- At the bottom of
collection-template.liquid, add this schema block:{% schema %} { "name": "Specific Collection Display", "settings": [ { "type": "collection", "id": "target_collection", "label": "Choose which collection to display" } ] } {% endschema %} - In the main part of your section, fetch the selected collection using the section setting:
{% assign target_collection = section.settings.target_collection %} {% if target_collection %} <h2>{{ target_collection.title }}</h2> {% for product in target_collection.products %} <!-- Your product markup --> {% endfor %} {% endif %}
Now you can go to your Shopify admin > Online Store > Themes > Customize, navigate to the collection page, and select any collection for this section via the editor.
Key Notes
- Always prefer handles over titles: Collection titles can be edited, but handles stay consistent—this prevents broken references if you rename a collection later.
- Check for collection existence: Wrapping your loop in an
{% if target_collection %}block avoids messy errors if the collection is deleted or misnamed. - Context matters: If your
collection-template.liquidis meant to display the current page's collection plus an additional specific collection, use Method 1 alongside your original{% for product in collection.products %}loop.
内容的提问来源于stack exchange,提问作者jhawes




