Thank you very much for your efforts Elliot, unfortunatly this didn’t work with the WordPress default gallery code. I’ve tried with this but it didn’t work:
<?php
$images = get_field('gallery');
$max = 5;
$i = 0;
if( $images ): ?>
<?php foreach( $images as $image ): $i++; ?>
<?php if( $i > $max){ break; } ?>
<?php
$shortcode = '[gallery columns="1" link="file" size="medium" ids="' . implode(',', $image_ids) . '"]';
echo do_shortcode( $shortcode ); ?>
<?php endforeach; ?>
<?php endif; ?>