1

I want to display some content only if a user from a certain group (wholesalers) is currently logged-in. The following syntax seems to work - but is there something more shorthand than the nested conditionals I'm using?

{% if currentUser %}
    {% if currentUser.isInGroup('wholesalers') %}
        Do something here...
    {% endif %}
{% endif %}
| improve this question | | | | |
  • Also, where do I find the group ID # for the user groups I've set-up? – Britchie Sep 1 '16 at 1:07
  • You can find that by navigating to the group settings in the CP and looking at the URL. It will be something like admin/settings/users/groups/4 the number is the group ID. – Luke Pearce Sep 1 '16 at 15:43
0

You could combine your conditions like so:

{% if currentUser and currentUser.isInGroup('wholesalers') %}
  Do something here...
{% endif %}
| improve this answer | | | | |
  • Thx @Luke Pearce – Britchie Sep 1 '16 at 15:39
1

You could use a switch statement or an if statement using getGroups().

Instead of me taking all the credit, here is a great thread along with an example of using getGroups at the template level.

Here is what that might look like within a plugin.

To get the actual ID of the group - I'm sure there is a more scientific way, but If I'm always going to know what the ID is, I'll just hover over the handle and look at the url. It will be something like /settings/users/groups/2.

Hope this helps!

| improve this answer | | | | |
  • Thx @Damon. Saw that. It'll be useful when I need to pull some other info. If the Craft folks are watching this thread, there's a feature request here to surface this is the UI. – Britchie Sep 1 '16 at 15:37

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.