0

This two-part question is half an exact duplicate of How do you display a user's 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

  • 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 Mar 9 '15 at 15:36
  • @BradBell I've attached a screenshot, hopefully I grabbed all relevant information. (copy img URL to view full-size) – Chase Giunta Mar 9 '15 at 17:14
  • If you {{ thePartner }} and {{ group }} what does it output in the browser? – Brad Bell Mar 9 '15 at 17:25
  • @BradBell {{ group }} returns the username "johnnyappleseed" . {{ thePartner }} gives me an Object of class Craft\ElementCriteriaModel could not be converted to string error – Chase Giunta Mar 9 '15 at 17:28
  • Ahh... add a .first() to the end of the craft.users.username line and see if that helps. – Brad Bell Mar 9 '15 at 17:30
2

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 %}
| improve this answer | |
  • 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 Mar 9 '15 at 18:24
  • 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 Mar 10 '15 at 18:58
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 %}
| improve this answer | |

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.