Visualizza le voci correlate su una pagina di categoria filtrata da un altro campo di categoria
Ho una struttura chiamata "Stoves". Ogni voce ha due campi di categoria "fuelType" e "output".
- Categorie del tipo di combustibile = "Legna da ardere", "Multicombustibile", "Gas" ed "Elettrico".
- Categorie di uscita = '4kW', '6kW', '8kW', '10kW',
Sul modello della pagina delle categorie il seguente semplice codice mi fornisce le voci corrette in base al campo "fuelType" ... ad esempio, quando visualizzo la pagina "Wood burning" mi mostra le stufe a legna.
{% set entries = craft.entries.relatedTo(category).all() %}
{% for entry in entries %}
<a href="{{ entry.url }}">{{ entry.title }}</a><br>
{% endfor %}
Ma ora devo anche scorrere il gruppo di categorie 'output' in modo da ottenere qualcosa di simile al seguente:
TUTTE le stufe a legna
Testo introduttivo
avviare il ciclo
Stufe da 4kW
Stove_4kW_No.1
Stove_4kW_No.2
Stove_4kW_No.3
Stufe da 6kW
Stove_6kW_No.1
Stove_6kW_No.2
Stufe 8kW
Stove_8kW_No.1
Stove_8kW_No.2
Stove_8kW_No.3
Stove_8kW_No.4
...e così via....
fine ciclo
È anche possibile? In alternativa, posso eseguire il pull con singole query di ingresso utilizzando i singoli slug della categoria "output"?
Grazie molto
Risposte
Normalmente useresti il filtro di gruppo per questo. A seconda di ciò di cui hai bisogno per l'output, assicurati di utilizzare anche il caricamento desideroso per migliorare le prestazioni.
{% 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 %}
MODIFICA: risposta alternativa di seguito per consentire di utilizzare il corretto ordinamento delle categorie secondo l'ordine della struttura della centrale.
Nota: questo approccio richiede il plugin 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 %}