Home › Forums › Add-ons › Gallery Field › Custom Fields in Gallery Array › Reply To: Custom Fields in Gallery Array
I see.. I think you should do something like this.. Assuming that I fetch the image ID correctly from the images array this should work 🙂
<?php
function buildGallery(){
$post_title = $_REQUEST['post_title'];
if($post_title){
$work = new WP_Query( array(
'post_type' => 'work',
'post_status' => 'publish',
'numberposts' => '-1',
'order' => 'asc',
'name' => $post_title
)
);
foreach ($work->posts as $work_post) {
$post_id = $work_post->ID;
}
}else{
//get the data from ajax() call
$post_id = $_REQUEST['post_id'];
}
$gallery_item = array(
"gallery_item" => array()
);
global $post;
$post = get_post($post_id);
$previous_post = get_previous_post();
$next_post = get_next_post();
//Fetch the gallery for the selected post
$gallery = get_field('gallery', $post_id);
if($gallery){
foreach($gallery as $image){ //loop through the images in the gallery
$constrain = get_field('constrain', $image['id']); //fetch our custom field
$image['constrain'] = $constrain; //Add it to the image array
}
}
array_push($gallery_item['gallery_item'], array(
"images" => $gallery, //output the new modified gallery array
"title" => get_the_title($post_id),
"content" => apply_filters( 'the_content', get_post_field('post_content', $post_id)),
"nextLink" => $next_post,
"prevLink" => $previous_post,
"id" => $post_id,
"postName" => $post->post_name,
"company" => get_field('company', $post_id),
"tags" => wp_get_post_tags($post_id)
));
// Return the String
die(json_encode($gallery_item));
}
?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.