WordPress 3.6 introduced quite a few new functions that are helpful when working with media attached to a post. One of my personal favorites is a helper function that lets you retrieve the URLs of images that have been inserted as parts of galleries in the post.

The function we will use is get_post_galleries_images(), which will return a multidimensional array of the image URLs belonging to all galleries in the specified post. It returns an array of arrays, one array for each gallery found.

The get_post_gallery_images() return value looks something like this:

Array
(
    [0] => Array
        (
            [0] => http://localhost/wpms/wp-content/uploads/edd/2013/05/02348_caltonhilledinburgh_1280x1024-190x120.jpg
            [1] => http://localhost/wpms/wp-content/uploads/2013/03/2560x1600_Space-190x120.jpg
            [2] => http://localhost/wpms/wp-content/uploads/2013/03/2649089468_abb2633bc6_o-190x120.jpg
            [3] => http://localhost/wpms/wp-content/uploads/2013/03/02156_orangedays_1024x1024-190x120.jpg
            [4] => http://localhost/wpms/wp-content/uploads/2013/03/02348_caltonhilledinburgh_1280x1024-190x120.jpg
        )

)

With this data, we can easily do something simple like this:

Our output is:

Screen Shot 2013-08-04 at 12.46.25 PM

The likely hood of you actually wanting to output the plain URLs is pretty slim, but this is just meant as an illustration of how you can use it. I can imagine the function being very helpful in scenarios where a developer needs to know a list of all assets used.

Note, this will return the URLs of all images included in galleries of the post; if you want to only retrieve images for the first gallery, you can use the get_post_gallery_images() function.

    • Pippin

      Sure enough, fixing!

  1. Matt

    Hey buddy. Good post. Right along the lines of what I am working on.

    I am using get_post_gallery which returns a multidimensional array that includes, among other things, the attachment ids of the images in the first gallery found in a post. Using get_post_galleries would return the same info for all galleries in the post.

    Depending on what one wants to do, this is maybe a little more useful because you can get other image sizes as well as things like a link to the full image using wp_get_attachment_url.

    Here’s the essence of what I am doing: http://snippi.com/s/cf61kn4

    Now to work on that regular expression… which I have but I’m not sure how to match the first only.

    btw… did you write the codex page?

    • Pippin

      Yours is definitely a more useful and realistic example 😀

      Yes I wrote the codex page.

    • Daphne

      you saved me. i was struggling for hours to fetch all gallery images for a post (and not just the ones attached to the post). thanks mate.

  2. Matt

    I am using this regular expression to remove the short code from the content: http://snippi.com/s/ypgvygd and it works great (though I need to figure out how to match the first only for my case).

    • Manda

      Wow nice and clean code Matt, it’s really help me solve my problem

  3. Wade Spane

    Looks like I can finally find some use for the wordpress gallery. I come from the design side of things and do a lot of sites for photographers and other designers. I’ve been using ACF for creating galleries and sliders and such but this looks great.,,

  4. Wordpressor

    Is it possible to change dimensions of these images in links with that function? For example replace these something-190×120.jpg thumbnails with full images? It’d be cool! Thanks!

  5. Steve

    hello Pippin,

    i wonder if your code could help me with a problem?

    I’m using the wordpress gallery (not a plugin), with ‘link to attachment page’ – and on the attachment page have next/previous links

    however, if i re-order the gallery the next/previous links don’t follow the order I’ve put them in.

    could the array script help in getting the next/previous links to follow the order of the gallery (i’m using lots of galleries across a number of pages, for a photographer, so I’d need it to be more general/global than specific)

    hope you can help as I’ve been looking for a solution for days now 🙂

  6. Kemeza

    Hey Pippin,

    i’m using this script for a custom gallery and though it works great for creating a gallery through the wysiwyg it seems to kill the usage of the_content(); within other pages that don’t call for a gallery.

    Now, for the pages where i use the wysiwyg for simple text i am able to use echo $post->post_content; to retrieve the wysiwyg content but i’m wondering why i am not able to use the_content();

    If you could point me in the right direction to figure that out i’m really curious.

    • Pippin

      In order to use `the_content()` you need to have the global $post object set up. It sounds like your global post object isn’t present so `the_content()` doesn’t know which item to pull content from.

      Take a look at this doc: https://codex.wordpress.org/Function_Reference/setup_postdata

  7. Luke R

    Excellent, this is great! Any suggestions on how to hide the original gallery items now that they’re being displayed with this? I’d like to use this loop to output gallery images with custom classes and data attributes, but if I just add this loop in after the_content, I of course end up with two versions of the galleries. I suppose I could hide the first with CSS, but that seems a little hacky… is there an easy way to prevent them coming through?

    • Pippin

      Instead of using `get_post_galleries_images( $post );` you could use the `get_attachments()` function that allows you to specify an `exclude` parameter.

Leave a Reply