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 %}
Britchie
  • 215
  • 2
  • 9
  • Also, where do I find the group ID # for the user groups I've set-up? – Britchie Sep 01 '16 at 01: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 01 '16 at 15:43

2 Answers2

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!

Damon
  • 4,706
  • 1
  • 20
  • 36
  • 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 01 '16 at 15:37
0

You could combine your conditions like so:

{% if currentUser and currentUser.isInGroup('wholesalers') %}
  Do something here...
{% endif %}
Luke Pearce
  • 3,863
  • 1
  • 11
  • 25