3

I'm nearly there on this one but could use some help. I want to list all User Groups, with all Users listed under each group. For bonus points, this all needs to be alphabetical (Group, then Users). Something like...

User Group A

  • User Name A
  • User Name B
  • etc...

User Group B

  • User Name A
  • User Name B
  • etc...

Here's what I have so far...

{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        <h2>{{ userGroup.name }}</h2>
        {# list all users for this group #}
    {% endfor %}
{% endif %}

I just can't figure out how to loop through users for a userGroup.

5

Something like this should work:

{% set userGroups = craft.userGroups.getAllGroups() %}

{% if userGroups|length %}
    {% for userGroup in userGroups %}
        <h2>
            {{ userGroup.name }}
        </h2>

        {% set users = craft.users.group(userGroup.handle) %}

        <ul>
        {% for user in users %}
            <li>
                {{ user.username }}
            </li>
        {% endfor %}
        </ul>
    {% endfor %}
{% endif %}
| improve this answer | |
1

Is this what you're going for?

{% set userGroups = craft.users.getAllGroups().order('firstName ASC') %}

{% if userGroups|length %}
    {{ userGroups.group }}

    {% for userGroup in userGroups %}
        <h2>{{ userGroup.name }}</h2>
        {# list all users for this group #}
    {% endfor %}
{% endif %}

This will output all of the Users in ABC order based on the firstName field.

| improve this answer | |
  • Hi guys, and thanks. Nearly there but {{ userGroups.group }} outputs nothing. – Ian Ebden May 24 '17 at 8:24

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.