2

I have a simple question about Assets. I want to output some HTML around a set of image IF there is more than one image. If there is just one image I would output different HTML. I know how to do this easily within EE's Assets but not sure how to do it in Craft's Assets.

In EE I would use something like:

{if photo:total_files > "1"} <!-- if there is more than one photo show a slideshow -->
<ul class="slider">
    <img scr="">
</ul>

{if:else} <!-- if its just one photo show it... -->
    <img scr="" class="single">
{/if}

I think I could target the first image but not sure how to construct the full conditional.

{% if entry.photos | length %}

{% set image = entry.assetFieldHandle.first() %}
<img src="{{ image.getUrl() }}" width="{{ image.getWidth() }}" height="{{ image.getHeight() }}">

{% endif %}

1 Answer 1

8

You're close!

{% set numberOfPhotos = entry.photos|length %}

{% if numberOfPhotos > 1 %}
  {# more than one #}
{% elseif numberOfPhotos == 1 %}
  {# exactly one #}
{% else %}
  {# no photos (always handle this case...) #}
{% endif %}
1
  • Thank you very much, Marion! This was a huge help. I almost had things working using a {{ loop.index }} conditional but yours works much better. Thanks again! Greatly appreciate the advice.
    – glider
    Jan 17, 2015 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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