Custom Fields
Re-Envisioned
Support MB Views Generating SRCSET code in Views/Twig

  • Creator
    Topic
  • #29272
    Resolved AMX
    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #29279
    Long 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
    AMX
    Participant

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

    Greetings,
    Tom

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