Generating SRCSET code in Views/Twig

Support MB Views Generating SRCSET code in Views/TwigResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #29272
    AMXAMX
    Participant

    Hi guys,

    How can I generate srcset sizes for Image Advanced field in Twig?

    In Toolset, I know there is a simple trick to achieve srcset - adding:

    class="size-full wp-image-1234" to the img tag somehow makes WordPress generate srcset automatically for image fields, if the wp-image- class has the correct image ID.

    However, this trick doesn't seem to work in Metabox Views.

    Is there a simple way to output automatically all srcset sizes in Views/Twig?

    Greetings,
    Tom

    #29279
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The field image_advanced supports showing srcset value. You can use it to assign to the attribute srcset of the image tag <img>

    {% for item in post.image_advanced %}
        <img src="{{ item.full.url }}" width="{{ item.full.width }}" height="{{ item.full.height }}" alt="{{ item.full.alt }}" srcset="{{ item.full.srcset }}">
    {% endfor %}

    Get more details on the documentation https://docs.metabox.io/fields/image-advanced/#template-usage

    #29281
    AMXAMX
    Participant

    Fantastic. It works. Thank you very much, Long.

    Greetings,
    Tom

    #46029

    I think there is a breaking change some where...

    This return me a list of the images but srcset are all set to false...
    rwmb_meta('advanced_image_key);

    #46030

    Never mind... it works!

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