6

I'm new to Craft and having a very hard time calling the URL from a field I created called imagePrimary

I've read this extensively but still no luck. https://craftcms.com/docs/assets-fields

{% include 'header.html' %}

{% block content %}

{% for entry in craft.entries.section('projects').limit(500).find() %}  

<div class="wrapper">

    <div class="row entry feed linked">

        <div class="title"><a href="{{ entry.url }}"><p><span>{{ entry.studio }}</span>{{ entry.title }}</p></a></div>

        <div class="col-xs-12 col-md-8">

            <div class="featured image" style="background-image: url(

                {% for asset in entry.imagePrimary %}
                     {{ asset.url() }}                                  
                {% endfor %}

            )"></div>

        </div>

        <div class="col-md-4 secondary">

            <div class="featured image secondary" style="background-image: url()"></div>
            <div class="featured image secondary" style="background-image: url()"></div>

        </div>

    </div>

</div>

{% endfor %}

{% endblock %}

{% include 'footer.html' %}
9

Hey Jeremy give this a shot to see if it works (from craft.assets)

{% for asset in entry.imagePrimary %}
     <img src="{{ asset.url }}" alt="{{ asset.title }}">                               
{% endfor %}

If this doesn't clear it up, I'd double check the field Handle to make sure it's spelled correctly and there is an image within one of your entries.

| improve this answer | |
  • 1
    Shouldn't {{ entry.title }} be {{ asset.title }}? – Mark Busnelli Jr Dec 8 '14 at 15:02
  • Yes you are correct. Editing to fix. – Adam McCombs Dec 8 '14 at 20:59
  • Why is iterating over the asset object necessary? Why doesn't entry.imagePrimary.url work? – emc Nov 29 '15 at 3:45
  • Got it - {{ entry.imagePrimary }} is an array! When defining an asset field, there is a field to denote how many elements this asset can hold. To access the first element in the array, use {{ entry.imagePrimary[0].url }} !! – emc Nov 29 '15 at 4:23

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.