0

This is my second website I am building with craft cms and I have no prior experience with Twig.

I have had a particular issue with echoing image assets from a singles entry.

The line I copied from my previous website with craft was;

<div id="teamsimagebg" style="background-image:url({% if entry.teamImage1 | length %}{% for asset in entry.teamImage1 %}{{ asset.url }}{% endfor %}{% endif %});"></div>

Field handles were updated correctly and yet on the template page the image is not displayed.

After looking into this I found the following;

{% set asset = entry.teamImage1.first() %}

{% if asset %}
    {{ asset.url }}
{% endif %}

This had no effect either.

2
  • 1
    It might be to do with the way in which you're checking the asset variable. In your original version you have {% if entry.teamImage1 | length %} whereas in the new one you are just going with {% if asset %}. Have a look at this answer on checking for empty fields- craftcms.stackexchange.com/questions/824/test-for-empty-field Commented Dec 21, 2014 at 17:30
  • Have you tried turning on devMode & {{ dump(asset) }} in your template. See if there's actually something in your asset variable. Commented Dec 24, 2014 at 9:42

1 Answer 1

1

Assuming you have an assets field with the fieldHandle teamImage1 then the following should work.

{% if entry.teamImage1|length %}
    {% set asset = entry.teamImage1.first() %}
    {{ asset.url }}
{% endif %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.