Created
July 30, 2019 21:22
-
-
Save aaronsummers/3ea3ce230aa0620010724f2dda0b4474 to your computer and use it in GitHub Desktop.
Get all images from a post
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
$post = get_post( get_the_ID() ); | |
$content = $post->post_content; | |
$regex = '/src="([^"]*)"/'; // Find all the images | |
preg_match_all( $regex, $content, $matches ); | |
$embedded_images_count = count( $matches[0] ); | |
echo '<pre>' . var_export($matches, true) . '</pre>'; | |
if ( $embedded_images_count > 0 ) { | |
for ( $i=0; $i < $embedded_images_count ; $i++ ) { | |
$img_src = $matches[0][$i]; | |
$trim_front = ltrim($img_src, 'src="'); | |
$trim_back = rtrim($trim_front, '"'); | |
$image_url = preg_replace("/-\d+[Xx]\d+\./", ".", $trim_back); // Trim the image dimentions from the image | |
$gallery_image_id = attachment_url_to_postid( $image_url ); | |
$caption = wp_get_attachment_caption( $gallery_image_id ); | |
$popup_image = wp_get_attachment_image_src($gallery_image_id, 'full')[0]; | |
$gallery_image = wp_get_attachment_image_src($gallery_image_id, 'medium')[0]; | |
echo $caption . '<br>' . $popup_image . '<br>' . $gallery_image . '<hr>'; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment