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', 
    'numberposts'    => 999 
) ); 
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?

1 Answer 1


You could use the get_post_galleries_images function, which returns an array of all the galleries from whatever post ID you specify. The following is a quick function that will display all the images:

function na_get_gallery_image_urls( $post_id, $number = false ) {

    $post = get_post($post_id);
    $count = 0;

    // Make sure the post has a gallery in it
    if( ! has_shortcode( $post->post_content, 'gallery' ) )

    // 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 ) {

            if ( $number == $count )

            echo '<img src="'.$image.'">';




If you put that in your functions.php file, you could then call the function with the ID like this: na_get_gallery_image_urls(128).

  • Thanks Nate, that's a fantastic way to do it, It hadn't occurred to me it could even be done that way. Is there anyway I can limit the number of images that appear? I forgot to add that to my original question, sorry! Oct 5, 2015 at 15:58
  • I'm glad it worked for you. I updated the code and added a "number" option. So if you only want 10 images, do na_get_gallery_image_urls(128, 10). If you don't specify an amount, it will return them all. @DavidAlsbright
    – Nate Allen
    Oct 5, 2015 at 17:40
  • Working perfectly, thanks again for your help, Nate. It's very much appreciated! Oct 5, 2015 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.