2

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?

4

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 %}
|improve this answer|||||
  • Clear answer, the answer of mmikkel helped me a bit in understanding why it works this way – noregt May 6 '15 at 7:40
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 %}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.