Skip to content

Instantly share code, notes, and snippets.

@zeropointdevelopment
Created December 27, 2013 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeropointdevelopment/8142737 to your computer and use it in GitHub Desktop.
Save zeropointdevelopment/8142737 to your computer and use it in GitHub Desktop.
[WordPress] Code from our blog post How to get Gallery Images in WordPress 3.5 - http://www.limecanvas.com/how-to-get-gallery-images-in-wordpress-3-5/
/** Grab IDs from new WP 3.5 gallery **/
function lc_grab_ids_from_gallery() {
global $post;
$attachment_ids = array();
$pattern = get_shortcode_regex();
$ids = array();
if (preg_match_all( '/'. $pattern .'/s', $post->post_content, $matches ) ) { //finds the "gallery" shortcode and puts the image ids in an associative array at $matches[3]
$count=count($matches[3]); //in case there is more than one gallery in the post.
for ($i = 0; $i < $count; $i++){
$atts = shortcode_parse_atts( $matches[3][$i] );
if ( isset( $atts[ids] ) ){
$attachment_ids = explode( ',', $atts[ids] );
$ids = array_merge($ids, $attachment_ids);
}
}
}
return $ids;
}
add_action( 'wp', 'lc_grab_ids_from_gallery' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment