Created
December 27, 2013 04:54
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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