2

I am using the code below to output a group of sections that have entries along with their section handle. How would I order the sections (ideally by structureId)? I'm not opposed to using supersort but I haven't been able to achieve the desired result.

{% set sections = craft.sections.getAllSections() %}
{% set entries = craft.entries.section(['first', 'second', 'third']) %}
{% set grouped = entries|group('section') %}

{% for sectionHandle, sectionEntries in grouped %}
    {% set groupSection = false %}
    {% for section in sections %}
        {% if section.name == sectionHandle %}
            {% set groupSection = section %}
        {% endif %}
    {% endfor %}

    {% if groupSection %}
        <h1>{{ groupSection.name }} - {{ groupSection.handle }}</h1>
    {% endif %}
{% endfor %}
| improve this question | | | | |
1
+25

Use SuperSort or write your own Twig filter for it. Craft/Twig don't come with a sort filter to sort by key and replicating the functionality (storing array keys, nested loops) is possible but kind of messy and also not performing as well.

I can understand the desire to not be reliant on too many plugin, but the functionality you need from SuperSort is simply a wrapper for a PHP function, so you can easily swap it out for another plugin if ever necessary.

It is also a great opportunity to start looking into plugin dev, write your first Twig extension or a simple Template Variable might just do it.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.