Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I know how to add images into my template:

{% set image = entry.featureImage.first() %}
<div class="imagewrapper">
    <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
</div>

But what if my client doesn't have feature images for all of his entries? I need some function that'll tell the website, if there is no image, omit <div class="imagewrapper">.

Perhaps some kind of if/else statement that I can use:

{% if
    //the featureimage for this entry is filled in
%}
    {% set image = entry.featureImage.first() %}
    <div class="imagewrapper">
        <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
    </div>
{% endif %}
share|improve this question

1 Answer 1

up vote 7 down vote accepted

You can use the length Twig filter for that.

{% if entry.featureImage | length %}
    {% set image = entry.featureImage.first() %}
    <div class="imagewrapper">
       <img src="{{ image.getUrl() }}" width="{{ image.getWidth('banner') }}" height="{{ image.getHeight('banner') }}">
    </div>
{% endif %}

If there isn't an image in featureImage then nothing will be output.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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