IF statement to hide custom term image when empty

Support MB Views IF statement to hide custom term image when empty

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #39174
    RoderickRoderick
    Participant

    Hi,

    I'm trying to figure out the IF statement to use for a custom term image. I wish to hide the image if the 'term image' field is not set. Because now it gives a 404 for this empty image.

    Can you help me with the correct IF settings for this type of field?

    <div class="header-image">
    {% set field = attribute( term, 'featured-image' ) %}
    <img src="{{ field.full.url }}" width="{{ field.full.width }}" height="{{ field.full.height }}" sizes="(max-width: 2500px) 100vw, 2500px" srcset="{{ field.medium.url }} 600w, {{ field.large.url }} 1675w, {{ field.full.url }} 2000w">
    </div>

    Thanks in advance,
    Roderick

    #39189
    PeterPeter
    Moderator

    Hello Roderick,

    You can check the field variable is not empty to display the image. Here is an example

    {% if field is not empty %}
        <img src="{{ field.full.url }}" width="{{ field.full.width }}" height="{{ field.full.height }}" sizes="(max-width: 2500px) 100vw, 2500px" srcset="{{ field.medium.url }} 600w, {{ field.large.url }} 1675w, {{ field.full.url }} 2000w">
    {% endif %}

    Refer to this topic https://support.metabox.io/topic/display-header-conditionally/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.