I have a custom field for a user which is an asset, it has the users larger profile photo.

In an entry template I can get their profile photo using:

{{ entry.author.getPhotoUrl }}

But I want to get the custom field.

Something like:

{{ entry.author.authorImage }}

I am sure I am missing something very simple?


Your answer with a for loop works but if you know there's only one image associated with that field, you can simplify your code using .first():

    <img src="{{ entry.author.authorImage|length ? entry.author.authorImage.first().getUrl() : '/images/userprofiledefault.jpg' }}" alt="{{ entry.author.getFullName }}">

Think of assets (and relationships in general) as a container that can hold more than one asset (or relationship). That's why you need to tell Craft which one you want—even if there's only one. Your for loop works because it's looping through that object (technically an ElementCriteriaModel object).

  • Ah perfect. Thanks for that. Will swap it out for that. – Gary Nov 7 '15 at 18:00
  • 1
    @Gary when using first you should check if it does return an asset model before accessing one of its properties! – carlcs Nov 7 '15 at 19:22
  • 1
    Good thought carlcs, updated my answer to include a fallback image. – RitterKnight Nov 7 '15 at 23:04

Ah it would of been handy if I had of entered the assets loop!

  {% for asset in entry.author.authorImage %}
    <img src="{{ asset.getUrl() }}" alt="{{ entry.author.getFullName }}">
  {% endfor %}

Your Answer

