4

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.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Ian Ebden
  • 894
  • 6
  • 12

2 Answers2

6

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 %}
henry
  • 228
  • 2
  • 9
Brad Bell
  • 67,440
  • 6
  • 73
  • 143
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.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
Mark Busnelli Jr
  • 926
  • 7
  • 18