Skip Navigation

[Resolved] Get page ID of repeatable image field in shortcode

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 1 reply, has 2 voices.

Last updated by Nigel 7 months, 3 weeks ago.

Author
Posts
#2700702
Group.jpg

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');

#2700978

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I didn't look too closely at your code, but I see that you appear to be trying to get the ID of the parent post of a repeatable field group instance using wp_get_post_parent_id, which won't work.

When you add a repeatable field group to some post type a new post type is created to store the group instances of field values.

For example, if I add a repeatable field group "Items" to some post type, then a post type with slug "items" is created to act as a container for each group of item fields. The "items" posts containing a group of fields are connected to the parent post through a Toolset post relationship (and the relationship also has a slug of "items").

If you have the ID of a field group post then you can get the ID of the parent using the toolset_get_related_post API function (see https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_post).

In my example where the repeatable field group is "items" I could get the parent with:

global $post;
$parent_id = toolset_get_related_post( $post->ID, 'items' );
#2700988

Hi Nigel,

It's work !!
As always, thank you for your invaluable help.

Regards,
Quentin