Wordpress 3.5: own gallery with included images doesn't work
I figured it out: If you use a gallery with images, which are already uploaded to the media library, the gallery shortcode looks like [gallery ids=1,2,3]
, what means, that images are only linked (and not attached) to the gallery, so post_type=attachment
doesn't work.
Now i'm using regular expressions to get the image IDs:
$post_content = $post->post_content;preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);$array_id = explode(",", $ids[1]);
It's now possible to pull all the galleries or even a single gallery using the $post->ID
and get_post_galleries()
. Each gallery will contain the image id list in ids
as well as a list of the image urls in src
. The gallery object is basically the shortcode arguments so you have access to those as well.
if ( $galleries = get_post_galleries( $post->ID, false ) ) { $defaults = array ( 'orderby' => 'menu_order ASC, ID ASC', 'id' => $post->ID, 'itemtag' => 'dl', 'icontag' => 'dt', 'captiontag' => 'dd', 'columns' => 3, 'size' => 'full', 'link' => 'file', 'ids' => "", 'src' => array (), ); foreach ( $galleries as $gallery ) { // defaults $args = wp_parse_args( $gallery, $defaults ); // image ids $args[ 'ids' ] = explode( ',', $args[ 'ids' ] ); // image urls $images = $args[ 'src' ]; }}