<p>First you need to get the entry for the main page. There are a number of ways you might do that (check the <a href="https://craftcms.com/docs/templating/craft.entries" rel="nofollow noreferrer">craft.entries</a> documentation). One would be to fetch it by section handle, so assuming the main page has section handle of <code>mainPage</code>, you can do this:</p>

<pre><code>{% set mainPage = craft.entries.section('mainPage').first %}
</code></pre>

<p>And then you can get the from the main page asset as before:</p>

<pre><code>{% for asset in mainPage.donateHeaderImage %}
  &lt;img src="{{ asset.url }}"&gt;
{% endfor %}
</code></pre>

<hr>

<p>To be safe, you should always handle the possibility that <code>craft.entries</code> won't return anything (maybe the mainPage section handle changed).</p>

<p>So either test it:</p>

<pre><code>{% set mainPage = craft.entries.section('mainPage').first %}
{% if mainPage %}
  {% for asset in mainPage.donateHeaderImage %}
    &lt;img src="{{ asset.url }}"&gt;
  {% endfor %}
{% endif %}
</code></pre>

<p>Or use the powerful <a href="https://twig.symfony.com/doc/2.x/templates.html#other-operators" rel="nofollow noreferrer">null-coalescing operator</a>:</p>

<pre><code>{% for asset in craft.entries.section('mainPage').first.donateHeaderImage ?? [] %}
  ...
</code></pre>

CC BY-SA 3.0
2017-10-10T23:15:21.440Z
Awesome you saved my butt again Marion! And the IF statement is just to be safe that craft.entries will return something, correct?
kingkelly
CC BY-SA 3.0
2017-10-11T14:16:17.863Z
That's right. It's a good habit to get into _always_ checking.
Marion Newlevant
CC BY-SA 3.0
2017-10-10T22:52:08.827Z
2017-10-10T22:52:08.827Z
Marion Newlevant
<p>I'm trying to display an asset that the client has selected to show up on the main page, and have it display on a totally different page. This sounds simple but I keep getting errors (and im learning).</p>

<p>Main page asset:</p>

<pre><code>{% for asset in entry.donateHeaderImage %}
   &lt;img src="{{ asset.url }}"&gt;
{% endfor %}
</code></pre>

<p>Not sure how to display that asset on a different page in Craft. </p>

<assets><entry>
Grabbing an asset field from Page A to display on Page B
# Grabbing an asset field from Page A to display on Page B

(Asked by: username_0 on 2017-10-10)

I'm trying to display an asset that the client has selected to show up on the main page, and have it display on a totally different page. This sounds simple but I keep getting errors (and im learning).
Main page asset:
{% for asset in entry.donateHeaderImage %}
   
{% endfor %}

Not sure how to display that asset on a different page in Craft.

**Tags:** assets, entry


---

## 1 answers

### Answer by username_1 on 2017-10-10. Score: 3 | Accepted answer

First you need to get the entry for the main page. There are a number of ways you might do that (check the craft.entries documentation). One would be to fetch it by section handle, so assuming the main page has section handle of mainPage, you can do this:
{% set mainPage = craft.entries.section('mainPage').first %}

And then you can get the from the main page asset as before:
{% for asset in mainPage.donateHeaderImage %}
  
{% endfor %}


To be safe, you should always handle the possibility that craft.entries won't return anything (maybe the mainPage section handle changed).
So either test it:
{% set mainPage = craft.entries.section('mainPage').first %}
{% if mainPage %}
  {% for asset in mainPage.donateHeaderImage %}
    
  {% endfor %}
{% endif %}

Or use the powerful null-coalescing operator:
{% for asset in craft.entries.section('mainPage').first.donateHeaderImage ?? [] %}
  ...

> **Comments:**
> **username_0**: Awesome you saved my butt again Marion! And the IF statement is just to be safe that craft.entries will return something, correct?  
> **username_1**: That's right. It's a good habit to get into _always_ checking.  

---

