如何在collection.rain中实现4级分类缩略图展示?
It sounds like your current template is only set up to render thumbnails up to the 3rd category level, and you need to extend it to show 4th-level items (like the individual exhibitions under /exhibitions/past/2019/) on their parent 3rd-level page. Let's walk through how to fix this:
First, Understand the Hierarchy
Your structure looks like this:
- Level 1:
exhibitions - Level 2:
past - Level 3:
2019(the page you want to show thumbnails on) - Level 4: Individual exhibitions (e.g.,
February-March, which already has images attached)
Why Your Previous Code Didn't Work
The {% for sub in subsubCat.subs %} loop likely failed because subsubCat isn't the correct variable referencing the current 3rd-level category (in this case, 2019). You need to target the current category's direct children (the 4th-level items) instead.
Step-by-Step Fix in collection.rain
- Open your
collection.raintemplate file - Locate the
<!-- 2nd/3rdlevel -->section of code - Add the following block inside this section (place it where you want the 4th-level thumbnails to appear on the 3rd-level page):
<!-- Display 4th-level category thumbnails on 3rd-level pages --> {% if currentCat.level == 3 and currentCat.subs is not empty %} <div class="category-thumbnails"> {% for childCat in currentCat.subs %} <!-- Only render if the 4th-level category has an image attached --> {% if childCat.image %} <div class="thumbnail-item"> <a href="{{ childCat.url }}"> <img src="{{ childCat.image.url }}" alt="{{ childCat.name }}" /> <span class="thumbnail-label">{{ childCat.name }}</span> </a> </div> {% endif %} {% endfor %} </div> {% endif %}
Key Adjustments to Match Your Template
- If your template uses a different variable name for the current category (e.g.,
collectioninstead ofcurrentCat), replacecurrentCatwith that variable. - Double-check the image field name: if your categories store thumbnails under
thumbnailinstead ofimage, updatechildCat.imagetochildCat.thumbnail. - Tweak the HTML classes (
category-thumbnails,thumbnail-item) to match your site's existing CSS styling so the thumbnails look consistent with other levels.
Test the Change
After saving the template, visit the 2019 past exhibitions page—you should now see thumbnails for all 4th-level exhibitions under 2019, just like how lower-level pages show their children.
内容的提问来源于stack exchange,提问作者Outer

