1

I want to setup a custom field to assign a URL for an image. On the front end I want to output the image and then wrap it in a URL. I've setup a field called 'logoURL' but not sure how to access that data. What I have isn't working and I've tried several variations.

{% set logo = { width: 175, height: 120, mode: 'fit' } %}

{% for image in entry.logos %}
    <li>
        <a href="{{ logos.logoURL }}"><img src="{{ image.getUrl(logo) }}" height="{{ image.getHeight(logo) }}" width="{{ image.getWidth(logo) }}" alt="{{ image.title }}"></a>          
    </li>
{% endfor %}
2

You’re close. When you do this:

{% for image in entry.logos %}
    ...
{% endfor %}

The code within the for-loop will be repeated for each asset that was assigned to your entry.logos field. Each time, the image variable will be assigned the current asset.

So, if you want to access a field that is on the asset (e.g. your logoUrl field), you would access that via image.logoUrl rather than logos.logoUrl. (You don’t actually have a variable named logos, so if you have Dev Mode enabled, that template would be giving you an error about that.)

{% set logo = { width: 175, height: 120, mode: 'fit' } %}

{% for image in entry.logos %}
    <li><a href="{{ image.logoURL }}"><img src="{{ image.getUrl(logo) }}" height="{{ image.getHeight(logo) }}" width="{{ image.getWidth(logo) }}" alt="{{ image.title }}"></a></li>
{% endfor %}
2
  • Thank you, Brandon! That worked. I thought I had tried that variation but alas. We're on our 7th Craft license and loving it.
    – glider
    Oct 9 '14 at 17:19
  • @David Glad to hear that! :) Oct 9 '14 at 17:24

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.