Ok, so here’s the right answer, with the full code for an ACF Repeater Field:
$count = get_post_meta( get_the_ID(), 'items', true );
if ( $count ) {
for ( $i = 0; $i < $count; $i++ ) {
$item_100x100 = intval( get_post_meta( get_the_ID(), 'items_' . $i . '_item_100x100', true ) );
$item_200x200 = intval( get_post_meta( get_the_ID(), 'items_' . $i . '_item_200x200', true ) );
$item_300x300 = intval( get_post_meta( get_the_ID(), 'items_' . $i . '_item_300x300', true ) );
if ( $item_100x100 ) {
$image_id = wp_get_attachment_image_src( $item_100x100, 'full' );
} elseif ( $item_200x200 ) {
$image = wp_get_attachment_image_src( $item_200x200, 'full' );
} elseif ( $item_300x300 ) {
$image = wp_get_attachment_image_src( $item_300x300, 'full' );
}
if ( $image ) {
echo '<a href="' . get_permalink() . '" rel="bookmark"><img src="' . $image[0] . '" alt="' . the_title_attribute( 'echo=0' ) . '">';
}
}
}
Or, instead the
echo
part, you can do:
printf( '<a href="https://wordpress.stackexchange.com/questions/261384/%s" rel="bookmark"><img src="https://wordpress.stackexchange.com/questions/261384/%s" alt="https://wordpress.stackexchange.com/questions/261384/%s" class="alignleft" /></a>', get_permalink(), $image[0], the_title_attribute( 'echo=0' ) );
You just have to change the ‘full’ to your desired image size.
UPDATE: As pointed out below by @bosco, wp_get_attachment_image
will return an entire HTML element. We just want the URL for the image to be able to set the src
attribute. Therefore, we’ll use wp_get_attachment_image_src
in our code. I realize I was messing with the HTML.