Hello,
On my WordPress page, I use a repeatable field 'post_type' => 'section-de-texte-1'. I also use a simple but repeatable image field named wpcf-gallerie-photo.
The idea is for the user to be able to add certain images to this wpcf-gallerie-photo field and then display them using the page builder section-de-texte-1, allowing them to choose exactly where to insert the images via a [GALERIE] shortcode.
This shortcode is linked to a function in function.php (below). The problem is that in the function, $post->ID is equal to the ID of the repeatable field section-de-texte-1 and not the current page. I can't figure out how to go up a level, meaning finding the relationship between the post_ID and the data-item-id="61".
Can you tell me how to find the ID of the page from function.php rather than the custom field ID?
// PHOTO GALLERY
function galerie_shortcode() {
global $post;
// Get the parent post ID if available
$parent_id = wp_get_post_parent_id($post->ID);
if (!$parent_id) {
// If the post has no parent, use its own ID
$parent_id = $post->ID;
}
// Display the parent post ID or the post's own ID
echo 'Page or parent ID: ' . $parent_id;
// Retrieve the image URLs from the parent post's metadata
$images = get_post_meta($parent_id, 'wpcf-gallerie-photo', false);
// Display retrieved data for debugging
echo '<pre>';
print_r($images);
echo '</pre>';
// If images are found, display them
if (!empty($images)) {
$output = '<div class="galerie">';
// Loop through the image URLs
foreach ($images as $image_url) {
// Get the image information
$image_data = wp_get_attachment_image_src(attachment_url_to_postid($image_url), 'full');
// Check the image format
$image_class = ($image_data[1] > $image_data[2]) ? 'Horizontal' : 'Vertical';
// Add the image with the appropriate class
$output .= '<img src="' . esc_url($image_url) . '" class="' . $image_class . '" />';
}
$output .= '</div>';
return $output;
}
return ''; // Return an empty string if there are no images
}
add_shortcode('GALERIE', 'galerie_shortcode');