2

I'm running into an issue here, hopefully someone can shed some light. I'm in a news category template,

{% for entry in craft.entries.section('news').relatedTo(category) %} 
  {% for image in news.newsimage %}
    <a href="{{ news.url }}"><img alt="{{ news.title }}" src="{{ news.url }}" /></a>
  {% endfor %}
  {{ entry.title }}
{% endfor %}

My entry.title, date, etc. all output correctly, I can't figure out how to output the image asset associated with newsimage.

3

It looks like you are trying to access a news variable that probably doesn't exist.

{% for entry in craft.entries.section('news').relatedTo(category) %} 

    {# Your image field will be found on your `entry` variable (not a `news` variable) #}
    {% for image in entry.newsimage %}

        {# One you are looping through your image field, 
           you can access the image properties via the `image` 
           variable you defined in your loop #}
        <a href="{{ image.getUrl() }}">
          <img alt="{{ image.title }}" src="{{ image.getUrl() }}" />
        </a>

    {% endfor %}

{% endfor %}

If you only have a single news image, you can grab that image directly using the first() method, instead of looping through your image field:

{% for entry in craft.entries.section('news').relatedTo(category) %} 

    {% set image = entry.newsimage.first() %}

    {% if image %}
        <a href="{{ image.getUrl() }}">
          <img alt="{{ image.title }}" src="{{ image.getUrl() }}" />
        </a>
    {% endif %}

{% endfor %}
| improve this answer | |
  • Thank makes complete sense... Thank you so much Ben.. – fidel Jun 27 '16 at 3:36

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.