I have a page within WordPress called 'Gallery', the page ID is 128, and I need to display only the gallery images from that page on a different page with a different ID. The images were uploaded using the standard WordPress gallery functionality.
I've been trying to use get_children
and a foreach
loop to achieve it, but I can't seem to get only the gallery images from the page I need (ID 128).
Here's what I have so far:
$images = get_children( array(
'post_parent' => 128,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
) );
if ( $images ) {
// looping through the images
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, 'full' );
}
}
How can I display gallery images from a WordPress page, on a different page?
This ended up being solved by Nate Allen over at the WordPress stack exchange.
this is in my functions.php:
function na_get_gallery_image_urls( $post_id ) {
$post = get_post($post_id);
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
echo '<img src="'.$image.'">';
}
}
}
And calling it on my page with this:
<?php na_get_gallery_image_urls(128); ?>
128 being the ID of the page with the Gallery attached.