Using Advanced Custom Fields with WordPress and Lightbox

There’s a lot going on here. First off, you should store the repeater field image as an ID (that probably requires changing how the subfield’s setting and possibly re-uploading some of your images) and then use all the WordPress Core API functions to handle it. (You’ll probably cross paths with wp_get_attachment_image_src.) That gets rid of your regex-ing, makes your image sizing take advantage of add_image_size, etc.

Second, you need to follow the repeater field’s code example on the page you linked to. get_repeater_field() returns an object you can iterate through with a loop-like while statement and get_sub_field(). (As an aside: get_field( 'a_repeater_field' ) returns an array you can iterate through with a foreach statement.)

Your final code will look something like this:

<?php if( get_field('image_gallery') ) {
    while( the_repeater_field('image_gallery') ) {
        $large_img_src = wp_get_attachment_image_src( get_subfield('an_image_subfield'), 'large' );
        echo '<a href="' . $large_img_src[0] . '">' . wp_get_attachment_image( get_sub_field('an_image_subfield'), 'thumbnail' ) . '</a>';
    }
} ?>

You can replace ‘thumbnail’ and ‘large’ with an array or a custom image size.

Leave a Comment