Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
get acf gallery using post id outside of loop
<?php
/*
Created by craig@jucra.com on 22/7/2022
The code below will get a list of photos in their
order as they appear in the ACF gallery and provide
you with the thumbnail and large versions.
You can use $gallery to output the gallery code.
You can also get the 1st main image.
*/
$images = get_post_meta( $post_id, "property_photos", $single = true );
$count = 0;
foreach($images as $img_id) {
$img_thumb = wp_get_attachment_image_src($img_id, $size = 'thumbnail');
$img_large = wp_get_attachment_image_src($img_id, $size = 'large');
$image_total_text = "REF: $ecpt_referencenumber <br>Photo $image_number of $total_images";
$gallery .= "
<a href='".$img_large[0]."'>
<img alt='".$image_total_text."' src='".$img_thumb[0]."'>
</a>";
//produce the main image
if($count == 0) { $main_image = $img_large[0]; }
$image_number = $image_number + 1;
$count = $count + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment