Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T06:16:38.640Z
Answers.Text: Nice and complete answer, but why do you use the `length` filter. The `first` method returns `null` if no element is found.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T06:29:02.957Z
Answers.Text: You'v right. You can use  {% if asset %} instead of {% if asset|length %} - the result should be the same.
Answers.UserDisplayName: Tom Bauer
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-04-22T05:16:01.440Z
Answers.LastActivityDate: 2016-04-22T05:16:01.440Z
Answers.UserDisplayName: Tom Bauer
Body: <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>
  
Tags: <assets><image-transforms>
Title: Best way to access getPrev and getNext asset fields
ThreadText: # 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 }) %}
      <a href="{{ prevEntry.url }}"><img alt="{{ prevEntry }}" class="img-responsive center-block" src="{{ asset.url }}">
  {% 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'
          } %}
  
          <a href="{{ prevEntry.url }}">
              <img src="{{ asset.getUrl(thumb) }}" class="img-responsive center-block"  alt="{{ prevEntry.title }}">
          </a>
  
      {% else %}
          <a href="{{ prevEntry.url }}">
              <img src="noImg.png" class="img-responsive center-block"  alt="{{ prevEntry.title }}">
          </a>    
  
      {% endif %}
  {% else %}
      <!-- There's no previous entry. -->
  {% 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.  
  
  ---
  
  