1

I'm making a staff page with a User field type, so you select all the users you want displayed. I'm having trouble showing each user's photo though. This works:

{% for user in block.staffList.all() %}
   Image URL: {{ craft.assets.id(user.photoId).one().url }}
{% endfor %}

But seems far from the ideal solution. Is there a more graceful way to do this?

1
  • Ok, this works: {{ user.photoUrl(user.id) }}, but still seems a little redundant
    – Bryce
    Sep 25 '20 at 20:54
2

It works very simply. Note that the photo field is an asset object, not an array of asset objects.

{% for user in block.staffList.all() %}
   Image URL: {{ user.photo.getUrl() }}
{% endfor %}
2
  • That's the ticket, thanks! Just a quick note: to skip over people that don't have photos, you can use {% if member.photo is not null %}
    – Bryce
    Sep 27 '20 at 17:53
  • @Bryce Of course, you can still do eager loading and caching {% for user in block.staffList.with(['photo']).all() %} . I gave a short answer, Craft CMS has many methods to optimize. Sep 28 '20 at 7:29

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.