How to hide image if file is empty in MB Views

Support MB Views How to hide image if file is empty in MB ViewsResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #40911
    RoderickRoderick
    Participant

    Hi, I've created a custom field for a term featured image. In MB Views I wish to hide the img tag if no image is selected/added. Unfortunately the following code doesn't work.

    Do you have an idea why this is not working?

    <div>
    {% if attribute( term, 'featured-image' ) is not empty %}
    {% set field = attribute( term, 'featured-image' ) %}
    <img src="{{ field.full.url }}" width="{{ field.full.width }}" height="{{ field.full.height }}" />
    {% endif %}</div>
    #40912
    RoderickRoderick
    Participant

    Apologies, here is the code again.

    <div>
    {% if attribute( term, 'featured-image' ) is not empty %}
    {% set field = attribute( term, 'featured-image' ) %}
    <img src="{{ field.full.url }}" width="{{ field.full.width }}" height="{{ field.full.height }}" />
    {% endif %}
    </div>
    #40922
    PeterPeter
    Moderator

    Hello,

    You can try to use this code to check if the feature image is not empty to display the image tag on the frontend.

    <div>
    {% if term.feature_image.full is not empty %}
    {% set field = term.feature_image.full %}
    <img src="{{ field.url }}" width="{{ field.width }}" height="{{ field.height }}" />
    {% endif %}
    </div>
    #41426
    RoderickRoderick
    Participant

    For future reference in case someone needs to find a solution. I managed to get this working by moving the 'if' condition after the 'set field' line and using 'field.full.url'.

    
    {% set field = attribute( term, 'featured-image' ) %}
    {% if field.full.url is not empty %}
    <img src="{{ field.full.url }}" width="{{ field.full.width }}" height="{{ field.full.height }}" />
    {% endif %}
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.