<p>If you need only one image, you can use the first() function instead of a for loop. </p>

<p>I recommend to check always if a image or entry exists, befor use it. Otherwise you risk to get an error. During development you should turn on the devMode, to see all errors (<a href="https://craftcms.com/docs/config-settings" rel="nofollow">https://craftcms.com/docs/config-settings</a>). </p>

<p>Here is a code example i would use in your case:</p>

<pre><code>{% if prevEntry %}

    {% set asset = prevEntry.featuredImage.first() %}

    {% if asset|length %}

        {% set thumb = {
            mode: 'crop',
            width: 200,
            height: 200,
            quality: 75,
            position: 'center-center'
        } %}

        &lt;a href="{{ prevEntry.url }}"&gt;
            &lt;img src="{{ asset.getUrl(thumb) }}" class="img-responsive center-block"  alt="{{ prevEntry.title }}"&gt;
        &lt;/a&gt;

    {% else %}
        &lt;a href="{{ prevEntry.url }}"&gt;
            &lt;img src="noImg.png" class="img-responsive center-block"  alt="{{ prevEntry.title }}"&gt;
        &lt;/a&gt;    

    {% endif %}
{% else %}
    &lt;!-- There's no previous entry. --&gt;
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-04-22T06:16:38.640Z
Nice and complete answer, but why do you use the `length` filter. The `first` method returns `null` if no element is found.
carlcs
CC BY-SA 3.0
2016-04-22T06:29:02.957Z
You'v right. You can use  {% if asset %} instead of {% if asset|length %} - the result should be the same.
Tom Bauer
CC BY-SA 3.0
2016-04-22T05:16:01.440Z
2016-04-22T05:16:01.440Z
Tom Bauer
<p>I am trying to access the previous and the next blog entries from my current post. Here is what I am doing</p>

<pre><code>{% set params = craft.entries.section('blog').order('postDate asc') %}

    {% set prevEntry = entry.getPrev(params) %}
    {% set nextEntry = entry.getNext(params) %}
    {# to access url, I use #}
    {{ prevEntry.url }}
    {# to access title, I use #}
    {{ prevEntry.title }}
</code></pre>

<p>The above works. However, I have an asset under the field handle "featuredImage". I need to access the image URL as well as I need to apply a transform. I am not sure the code below is the most efficient way to do this. Also the image transform is giving me an error (server returns 500 error)</p>

<pre><code>{% for asset in prevEntry.featuredImage %}
    {% do asset.setTransform({ mode:crop, width: 200, height: 200 }) %}
    &lt;a href="{{ prevEntry.url }}"&gt;&lt;img alt="{{ prevEntry }}" class="img-responsive center-block" src="{{ asset.url }}"&gt;
{% endfor %}
</code></pre>

<p>Is looping the best way to access the data considering that there is only one image? Is there a better way? Image transform is also not working (not sure why).     </p>

<assets><image-transforms>
Best way to access getPrev and getNext asset fields
# Best way to access getPrev and getNext asset fields

(Asked by: username_0 on 2016-04-21)

I am trying to access the previous and the next blog entries from my current post. Here is what I am doing
{% set params = craft.entries.section('blog').order('postDate asc') %}

    {% set prevEntry = entry.getPrev(params) %}
    {% set nextEntry = entry.getNext(params) %}
    {# to access url, I use #}
    {{ prevEntry.url }}
    {# to access title, I use #}
    {{ prevEntry.title }}

The above works. However, I have an asset under the field handle "featuredImage". I need to access the image URL as well as I need to apply a transform. I am not sure the code below is the most efficient way to do this. Also the image transform is giving me an error (server returns 500 error)
{% for asset in prevEntry.featuredImage %}
    {% do asset.setTransform({ mode:crop, width: 200, height: 200 }) %}
    
{% endfor %}

Is looping the best way to access the data considering that there is only one image? Is there a better way? Image transform is also not working (not sure why).

**Tags:** assets, image-transforms


---

## 1 answers

### Answer by username_1 on 2016-04-22. Score: 2 | Accepted answer

If you need only one image, you can use the first() function instead of a for loop. 
I recommend to check always if a image or entry exists, befor use it. Otherwise you risk to get an error. During development you should turn on the devMode, to see all errors (https://craftcms.com/docs/config-settings). 
Here is a code example i would use in your case:
{% if prevEntry %}

    {% set asset = prevEntry.featuredImage.first() %}

    {% if asset|length %}

        {% set thumb = {
            mode: 'crop',
            width: 200,
            height: 200,
            quality: 75,
            position: 'center-center'
        } %}

        
            
        

    {% else %}
        
            
            

    {% endif %}
{% else %}
    
{% endif %}

> **Comments:**
> **username_2**: Nice and complete answer, but why do you use the `length` filter. The `first` method returns `null` if no element is found.  
> **username_1**: You'v right. You can use  {% if asset %} instead of {% if asset|length %} - the result should be the same.  

---

