แสดงรายการที่เกี่ยวข้องบนหน้าหมวดหมู่ที่กรองโดยเขตข้อมูลหมวดหมู่อื่น

Aug 20 2020

ฉันมีโครงสร้างที่เรียกว่า "Stoves" แต่ละรายการมีฟิลด์ประเภทสองฟิลด์ 'fuelType' และ 'output'

  • ประเภทเชื้อเพลิง = '' การเผาไหม้ของไม้ ',' เชื้อเพลิงหลายชนิด ',' ก๊าซ 'และ' ไฟฟ้า '
  • ประเภทเอาต์พุต = '4kW', '6kW', '8kW', '10kW',

ในเทมเพลตหน้าหมวดหมู่รหัสง่ายๆต่อไปนี้จะให้รายการที่ถูกต้องตามฟิลด์ "fuelType" ... เช่นเมื่อดูหน้า "การเผาไม้" จะแสดงเตาเผาไม้ให้ฉันเห็น

{% set entries = craft.entries.relatedTo(category).all() %}

{% for entry in entries %}
    <a href="{{ entry.url }}">{{ entry.title }}</a><br>
{% endfor %}

แต่ตอนนี้ฉันต้องยังห่วงผ่าน 'ส่งออก' กลุ่มหมวดหมู่เพื่อให้ฉันได้รับสิ่งที่ต้องการต่อไปนี้:

เตาเผาไม้ทั้งหมด

ข้อความแนะนำ

เริ่มลูป

  • เตา 4kW

  • Stove_4kW_No.1

  • เตา _4kW_No.2

  • Stove_4kW_No.3

  • เตา 6kW

  • เตา _6kW_No.1

  • เตา _6kW_No.2

  • เตา 8kW

  • เตา _8kW_No.1

  • Stove_8kW_No.2

  • Stove_8kW_No.3

  • Stove_8kW_No.4

... และอื่น ๆ ....

สิ้นสุดลูป

เป็นไปได้หรือไม่? หรืออีกวิธีหนึ่งที่ฉันสามารถดึงคำค้นหาแต่ละรายการโดยใช้ slugs หมวดหมู่ 'output' แต่ละรายการ

ขอบคุณมาก

คำตอบ

1 JamesSmith Aug 20 2020 at 02:20

โดยทั่วไปคุณจะใช้ตัวกรองกลุ่มสำหรับสิ่งนี้ ขึ้นอยู่กับสิ่งที่คุณต้องการส่งออกอย่าลืมใช้การโหลดอย่างกระตือรือร้นเพื่อปรับปรุงประสิทธิภาพ

{% set groupedEntries = craft.entries.with(['yourOutputCatFieldHandle']).relatedTo(category).all()|group('yourOutputCatFieldHandle[0].title') %}

{% for cat, entries in groupedEntries %}
    <h3>{{ cat }}</h3>
    <ul>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>
{% endfor %}

แก้ไข: คำตอบทางเลือกด้านล่างเพื่อให้สามารถใช้ลำดับหมวดหมู่ที่ถูกต้องตามลำดับโครงสร้างของแผงควบคุม

หมายเหตุ: วิธีนี้ต้องใช้ปลั๊กอินsupersort

{# ======================================
First, fetch entries related to this category,
then group the array by each category's
`lft` structure position, then sort the array
by those keys with supersort's ksort function
========================================= #}

{% set groupedEntries = craft.entries.with([
        'yourOutputCatFieldHandle'
    ])
    .relatedTo(category)
    .all()|group('yourOutputCatFieldHandle[0].lft')|supersort('ksort')
%}

{# ======================================
Next, create a hash map of those categories so we can
match up the `lft` left structure position
with the category's title later.
(google "Nested Sets" if you're really bored)
========================================= #}

{% if groupedEntries|length %}
    {% set catTitlesMap = craft.categories.group('yourOutputCatGROUPHandle').all()|group('lft') %}
{% endif %}

{# ======================================
Finally, loop through the grouped array,
matching up the accessory's `lft` position
with the hash map to get the right title...
========================================= #}

{% for cat, entries in groupedEntries %}
    <h3>{{ catTitlesMap[cat][0].title }}</h3>
    <ul>
        {% for entry in entries %}
            <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
        {% endfor %}
    </ul>
{% endfor %}