Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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: {% ???? %}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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 }}
share|improve this answer
    
Perfect, thank you -- that did the trick nicely. –  Shanan Galligan Aug 2 at 18:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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