Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-06T07:40:04.740Z
Answers.Text: Clear answer, the answer of mmikkel helped me a bit in understanding why it works this way
Answers.UserDisplayName: noregt
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-05T14:55:26.640Z
Answers.LastActivityDate: 2015-05-05T14:55:26.640Z
Answers.UserDisplayName: carlcs
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-05T15:35:03.537Z
Answers.LastActivityDate: 2015-05-05T15:35:03.537Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Body: <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>
  
Tags: <assets><conditional><segments>
Title: Setting a variable assets field, depending on the URL?
ThreadText: # 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`).
  
  @username_1' answer should do it, though you could also set the variable w/ a [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators) to make it a one-liner:
  
  ```
  {% set assetField = craft.request.getSegment(1) == 'special' ? entry.assetFieldSpecial : entry.assetField %}
  
  {% for assets in assetField %}
      ....
  {% endfor %}
  
  ```
  
  
  ---
  
  