1

I am pulling assets from the work section like this-

<div class="header_image">
  {% for entry in craft.entries.section('work').limit(5) %}
            <a href="{{ entry.url }}">  
            <div class="mainImage" style="background-image:url({{ asset.url }})"> </div>
            </a> 
  {% endfor %}

The Asset isn't appearing on the page but if I hover over where the image should, the link is working properly and I can to get that entry. The asset.url isn't working and I'm not sure what else it should be. Any ideas?

2

Make sure you know the asset exists first:

{% if entry.asset.first() %}
  <div class="mainImage" style="background-image:url({{ entry.asset.first().url }})"></div>
{% endif %}

Another nifty trick is to enable "dev mode". This will give you detailed stack traces about what's going wrong in your instance!

http://buildwithcraft.com/docs/config-settings#devMode

| improve this answer | |
  • that didn't work. but dev mode is telling me it's this line- <div class="mainImage" style="background-image:url({{ asset.url }})"> </div> – user1949733 Nov 10 '15 at 2:04
  • Is asset the fieldname you've used? It should be attached to the entry object as well. So it should look like {{ entry.yourCustomFieldname.first().url }}. – Rob Erskine Nov 10 '15 at 16:42
1

Your issue is that you are calling asset.url rather than entry.asset.first.url. The variable 'asset' does not exist. The asset is a field of the entry, and must be referenced as such.

Edit: I assumed your field name was called 'asset'. You should be calling entry.assetFieldHandle.first.url.

| improve this answer | |
  • Are you saying that I should have this - <div class="mainImage" style="background-image:url({{ entry.asset.first.url }})"> </div> because that doesn't work. I had this working perfectly before, I can't figure out what has gone wrong. – user1949733 Nov 10 '15 at 3:04
  • So, the variable asset doesn't exist. But if I replace asset.url with entry.asset.first.url, I get 'do not have a method or closure named "asset" – user1949733 Nov 10 '15 at 3:17
  • what is the name of your asset field? You'll access the asset via entry.<assetFieldName> – kcolls Nov 10 '15 at 4:39
  • So it should be this? <div class="header_image"> {% for entry in craft.entries.section('work').limit(5) %} <a href="{{ entry.url }}"> <div class="mainImage" style="background-image:url({{ entry.asset('mainImages').url }} )"> </div> </a> {% endfor %} </div> – user1949733 Nov 10 '15 at 15:06
  • Try entry.mainImages.first.url – kcolls Nov 10 '15 at 15:30

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.