Loop images in "image_advanced" field using Twig

Support MB Blocks Loop images in "image_advanced" field using Twig

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #33719
    Stefano ThiellaStefano Thiella
    Participant

    Hello, I'm trying to create a custom gallery block.

    I have an "image_advanced" field called "colori" and I want to loop thourgh each image using a {% for %} twig loop. I tried this but it's not working.

    {% for item in colori %}<div class="test">{{ item }}</div>{% endfor %}

    #33784
    Long NguyenLong Nguyen
    Moderator

    Hi Stefano,

    You can use the helper function mb_get_block_field() to get the block field value then use a loop to iterate through an array of images. For example:

    {% set images = mb.mb_get_block_field( 'image_advanced' ) %}
    {% for image in images %}
        <img src="{{ image.full_url }}" />
    {% endfor %}

    Read more on the documentation
    https://docs.metabox.io/extensions/mb-blocks/#render_callback
    https://docs.metabox.io/fields/image-advanced/#template-usage

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