Get the first image only

Support MB Views Get the first image onlyResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #38316
    Guillaume LORAINGuillaume LORAIN
    Participant

    Hello, I'm a new user from Toolset.
    I would like to display only the first image from an advanced image field in a MB view.

    To display the urls of all the images this code works well.
    {% for item in post.image_advanced_jz5jk5t1wfd %}
    {{ item.medium.url }}
    {% endfor %}

    Is it possible to add a limit to see only the first item ?

    I've seen this code on the documentation :
    <?php $images = rwmb_meta( 'my_field_id', ['limit' => 1] ) ?>
    <?php $image = reset( $images ) ?>
    ">

    I tried to update it to make work in view but I got an error.

    Thanks.

    #38318
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can use this code in the View template to display the first image of the list images without using the loop.

    {% set images = post.image_advanced %}
    {% set item = mb.current( images ) %}
    
    <img src="{{ item.large.url }}" width="{{ item.large.width }}" height="{{ item.large.height }}" alt="{{ item.large.alt }}">

    Refer to the PHP documentation https://www.php.net/manual/en/function.current.php
    and ours https://docs.metabox.io/extensions/mb-views/#running-php-functions

    #38319
    Guillaume LORAINGuillaume LORAIN
    Participant

    Thank you.

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