2

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?

1

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).

|improve this answer|||||
  • 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
1

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 %}
|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.