<p>If you know the user will only belong to one group, something like this should do it.</p>

<pre><code>{% set user = craft.users.username(craft.request.getSegment(2)).first() %}

{# make sure we found a user %}
{% if user|length %}
    {% set groups = user.getGroups() %}

    {# make sure they belong to at least one group #}
    {% if groups|length %}
        {# display the name of the first group #}
        {{ groups[0].name }}
    {% endif %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-09T18:24:15.720Z
Thanks Brad! That worked. So, is there any clarification you can give as to why the previous example in the other thread didn't work for my case?
Chase Giunta
CC BY-SA 3.0
2015-03-10T18:58:23.377Z
Btw, something like this (checking for user group based on URL) seems like it should be a simple task (obviously it's not) & useful example to have in the docs. Along with a ton of others!
Chase Giunta
CC BY-SA 3.0
2015-03-09T18:20:01.397Z
2015-03-09T18:20:01.397Z
Brad Bell
<p>If you only want to get the user if they are A) in the group identified in the 1st segment, and B) have the username in the 2nd segment, you can do this:</p>

<pre><code>{% set user = craft.users({
    group:    craft.request.getSegment(1),
    username: craft.request.getSegment(2)
}).first() %}

{# Make sure we found someone #}
{% if not user %}
    {% exit 404 %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-03-13T20:48:27.120Z
2015-03-13T20:48:27.120Z
Brandon Kelly
<p>This two-part question is half an exact duplicate of <a href="https://craftcms.stackexchange.com/questions/1578/how-do-you-display-a-users-user-group">How do you display a user&#39;s User Group?</a> but I'm unable to leave a comment :(</p>

<p>I'm trying to display &amp; check-for the user group a user belongs to on a public profile page. There are coaches and players.</p>

<p>The URL structure is <code>domain.com/coaches/johnnyappleseed</code></p>

<p>Copying the exact example code given in the original question (link above) seems to just return the User's full name, not the group the user belongs to. What could I be missing?</p>

<p>Also, if johnnyapplseed is a coach, I can still access his profile page by going to <code>domain.com/players/johnnyappleseed</code> which is why I'm trying to check for the usergroup to begin with. Is checking for the usergroup in each player &amp; coach profile template the correct way to do this?</p>

<p>UPDATE: I've attached a screenshot of all the information I think could possibly be relevant. This has to be something simple I'm overlooking!</p>

<p><img src="https://i.sstatic.net/3CtYV.jpg" alt="enter image description here"></p>

CC BY-SA 3.0
2015-03-09T15:36:43.800Z
Can you edit your question and share the template code you're using?  The first example in the link you shared should definitely display the user groups a user belongs to.
Brad Bell
CC BY-SA 3.0
2015-03-09T17:14:06.877Z
@BradBell I've attached a screenshot, hopefully I grabbed all relevant information. (copy img URL to view full-size)
Chase Giunta
CC BY-SA 3.0
2015-03-09T17:25:18.230Z
If you `{{ thePartner }}` and `{{ group }}` what does it output in the browser?
Brad Bell
CC BY-SA 3.0
2015-03-09T17:28:25.523Z
@BradBell `{{ group }}` returns the username "johnnyappleseed" . `{{ thePartner }}` gives me an
`Object of class Craft\ElementCriteriaModel could not be converted to string` error
Chase Giunta
CC BY-SA 3.0
2015-03-09T17:30:00.143Z
Ahh... add a `.first()` to the end of the `craft.users.username` line and see if that helps.
Brad Bell
CC BY-SA 3.0
2015-03-09T17:36:20.190Z
@BradBell http://cl.ly/image/2C1u1x1w2n0t ? Did I add that in the correct place?
Chase Giunta
<user-groups>
Display & check-for User Group on public profile
# Display & check-for User Group on public profile

(Asked by: username_0 on 2015-03-09)

This two-part question is half an exact duplicate of [How do you display a user's User Group?](https://craftcms.stackexchange.com/questions/1578/how-do-you-display-a-users-user-group) but I'm unable to leave a comment :(

I'm trying to display & check-for the user group a user belongs to on a public profile page. There are coaches and players.

The URL structure is `domain.com/coaches/johnnyappleseed`

Copying the exact example code given in the original question (link above) seems to just return the User's full name, not the group the user belongs to. What could I be missing?

Also, if johnnyapplseed is a coach, I can still access his profile page by going to `domain.com/players/johnnyappleseed` which is why I'm trying to check for the usergroup to begin with. Is checking for the usergroup in each player & coach profile template the correct way to do this?

UPDATE: I've attached a screenshot of all the information I think could possibly be relevant. This has to be something simple I'm overlooking!

![enter image description here](https://i.sstatic.net/3CtYV.jpg)

**Tags:** user-groups
> **Comments:**
> **username_1**: Can you edit your question and share the template code you're using?  The first example in the link you shared should definitely display the user groups a user belongs to.  
> **username_0**: @BradBell I've attached a screenshot, hopefully I grabbed all relevant information. (copy img URL to view full-size)  
> **username_1**: If you `{{ thePartner }}` and `{{ group }}` what does it output in the browser?  
> **username_0**: @BradBell `{{ group }}` returns the username "johnnyappleseed" . `{{ thePartner }}` gives me an `Object of class Craft\ElementCriteriaModel could not be converted to string` error  
> **username_1**: Ahh... add a `.first()` to the end of the `craft.users.username` line and see if that helps.  
> **username_0**: @BradBell http://cl.ly/image/2C1u1x1w2n0t ? Did I add that in the correct place?  

---

## 2 answers

### Answer by username_1 on 2015-03-09. Score: 2 | Accepted answer

If you know the user will only belong to one group, something like this should do it.
{% set user = craft.users.username(craft.request.getSegment(2)).first() %}

{# make sure we found a user %}
{% if user|length %}
    {% set groups = user.getGroups() %}

    {# make sure they belong to at least one group #}
    {% if groups|length %}
        {# display the name of the first group #}
        {{ groups[0].name }}
    {% endif %}
{% endif %}

> **Comments:**
> **username_0**: Thanks Brad! That worked. So, is there any clarification you can give as to why the previous example in the other thread didn't work for my case?  
> **username_0**: Btw, something like this (checking for user group based on URL) seems like it should be a simple task (obviously it's not) & useful example to have in the docs. Along with a ton of others!  

---

### Answer by username_2 on 2015-03-13. Score: 1

If you only want to get the user if they are A) in the group identified in the 1st segment, and B) have the username in the 2nd segment, you can do this:
{% set user = craft.users({
    group:    craft.request.getSegment(1),
    username: craft.request.getSegment(2)
}).first() %}

{# Make sure we found someone #}
{% if not user %}
    {% exit 404 %}
{% endif %}


---

