<p>I'd probably do it like so:</p>

<pre><code>{% if craft.request.getSegment(1) == 'special' %}
    {% set assets = entry.assetfield_1 %}
{% else %}
    {% set assets = entry.assetfield_2 %}
{% endif %}

{% for asset in assets %}
    ...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-06T07:40:04.740Z
Clear answer, the answer of mmikkel helped me a bit in understanding why it works this way
noregt
CC BY-SA 3.0
2015-05-05T14:55:26.640Z
2015-05-05T14:55:26.640Z
carlcs
<p>The issue with your original code is that you're looping through the "default" field, i.e. <code>entry.assetfield</code>, not the variable you set (<code>assetfield</code>).</p>

<p>@carlcs' answer should do it, though you could also set the variable w/ a <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow">ternary operator</a> to make it a one-liner:</p>

<pre><code>{% set assetField = craft.request.getSegment(1) == 'special' ? entry.assetFieldSpecial : entry.assetField %}

{% for assets in assetField %}
    ....
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-05T15:35:03.537Z
2015-05-05T15:35:03.537Z
Mats Mikkel Rummelhoff
<p>How can I set different assets fields depending on the URL?</p>

<p>I tried:</p>

<pre><code>{% if craft.request.getSegment(1) == 'special' %}
   {% set assetfield = variantfield1 %}
{% endif %}

{% for asset in entry.assetfield %}     
     ...
{% endfor %} 
</code></pre>

<p>But that doen't change <code>entry.assetfield</code>?</p>

<assets><conditional><segments>
Setting a variable assets field, depending on the URL?
# Setting a variable assets field, depending on the URL?

(Asked by: username_0 on 2015-05-05)

How can I set different assets fields depending on the URL?
I tried:
{% if craft.request.getSegment(1) == 'special' %}
   {% set assetfield = variantfield1 %}
{% endif %}

{% for asset in entry.assetfield %}     
     ...
{% endfor %} 

But that doen't change entry.assetfield?

**Tags:** assets, conditional, segments

---

## 2 answers

### Answer by username_1 on 2015-05-05. Score: 4 | Accepted answer

I'd probably do it like so:
{% if craft.request.getSegment(1) == 'special' %}
    {% set assets = entry.assetfield_1 %}
{% else %}
    {% set assets = entry.assetfield_2 %}
{% endif %}

{% for asset in assets %}
    ...
{% endfor %}

> **Comments:**
> **username_0**: Clear answer, the answer of mmikkel helped me a bit in understanding why it works this way  

---

### Answer by username_2 on 2015-05-05. Score: 4

The issue with your original code is that you're looping through the "default" field, i.e. entry.assetfield, not the variable you set (assetfield).
@carlcs' answer should do it, though you could also set the variable w/ a ternary operator to make it a one-liner:
{% set assetField = craft.request.getSegment(1) == 'special' ? entry.assetFieldSpecial : entry.assetField %}

{% for assets in assetField %}
    ....
{% endfor %}


---

