Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created July 30, 2019 21:22
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 aaronsummers/3ea3ce230aa0620010724f2dda0b4474 to your computer and use it in GitHub Desktop.
Save aaronsummers/3ea3ce230aa0620010724f2dda0b4474 to your computer and use it in GitHub Desktop.
Get all images from a post
$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