4

This seems simple, but I cannot get it to render -- how do display a user's User Group? Assuming I set the user in segment 2:

{% set thePartner = craft.users.username( craft.request.getSegment(2) ) %}
This Is My User Group: {% ???? %}

1 Answer 1

3

The UserModel has a getGroups() function which:

Returns an array of UserGroupModel objects that the user belongs to.

Since a user can belong to multiple usergroups, you have to loop through the array containing them:

{% set thePartner = craft.users.username( craft.request.getSegment(2) ) %}

# Loop through array of UserGroupModels #
{% for group in thePartner.getGroups() %}
    # Print name of current UserGroupModel #
    {{ group.name }}
{% endfor %}

If you are sure the user only belongs to one group, you can fetch the first UserGroupModel from like this:

{% set thePartner = craft.users.username( craft.request.getSegment(2) ) %}

# Get first UserGroupModel #
{% set group = thePartner.getGroups().first %}

# Print name #
{{ group.name }}
1
  • 1
    Perfect, thank you -- that did the trick nicely. Commented Aug 2, 2014 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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