1

I have two problems with CraftCMS :(

First one - I want to make images gallery and now using something like this:

     {% set image = entry.car_photos[0] %}
{% if image %}<div class="cSlider__item">
     <img class="is-rounded" src="{{ image.url }}">
</div>{% endif %}
        {% set image = entry.car_photos[1] %}
{% if image %}<div class="cSlider__item">
     <img class="is-rounded" src="{{ image.url }}">
</div>{% endif %}
    {% set image = entry.car_photos[2] %}
{% if image %}<div class="cSlider__item">
     <img class="is-rounded" src="{{ image.url }}">
</div>{% endif %}

The problem is that if the third image doesn't exist - I get the error. How can I make it work?

And second problem... I have field with my entries with number in it. So if the entry have that field with value "2" - I want to show text "On sale", if value "1" - I want to have "Out of stock". I tried with documentation but it doesn't help me... I tried something like this:

{% set soon = craft.entries()
    .Availability_Status('2')
    .all() %}
{% if soon %}
<div style="float: right; display: inline-block; margin-top: -1rem;"> <div class="buttons"><button class="button is-warning is-normal">Еще не объявлено</button></div></div>
{% endif %}

{% set out = craft.entries()
    .Availability_Status('1')
    .all() %}
{% if out %}
<div style="float: right; display: inline-block; margin-top: -1rem;"> <div class="buttons"><button class="button is-warning is-normal">Еще не объявлено</button></div></div>
{% endif %}

Despite the fact that the value in that field with the current entry is "2", I'm getting both buttons in template.

1

It looks like you have 2 questions here?

1- why not run them through a loop? it will then only display the amount of images you have.

eg;

{% for images in entry.car_photos.all() %}
<div class="cSlider__item">
  <img class="is-rounded" src="{{ image.url }}">
</div>
{% endfor %}

Second issue (probably should have had a second question for this).

You are pulling all of the entries out that have that status set and then displaying your if div if the query has succeeded. As long as there is any entry with that status that command will ALWAYS succeed.

In pseudocode what you are currently doing is this

  • Get all of the entries with a status of 2
  • if you got all of the entries with a status of 2 entries show this div

What you need to do is loop through your entries and display that div IF that status matches.

Example:

{% for entries in craft.entries.all() %}
  
{% if entries.Availability_Status('1') %}
<div style="float: right; display: inline-block; margin-top: -1rem;"> <div class="buttons"><button class="button is-warning is-normal">Еще не объявлено</button></div></div>
{% endif %}

{% endfor %}

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.