Created
August 5, 2013 05:06
-
-
Save vmunhoz/6153636 to your computer and use it in GitHub Desktop.
Retrieve images from wordpress 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
<?php if (have_posts()) : ?> | |
<?php while (have_posts()) : the_post(); ?> | |
<?php | |
$szPostContent = $post->post_content; | |
$szSearchPattern = '~<img [^\>]*\ />~'; | |
// Run preg_match_all to grab all the images and save the results in $aPics | |
preg_match_all( $szSearchPattern, $szPostContent, $aPics ); | |
// Check to see if we have at least 1 image | |
$iNumberOfPics = count($aPics[0]); | |
if ( $iNumberOfPics > 0 ) { | |
// Now here you would do whatever you need to do with the images | |
// For this example the images are just displayed | |
for ( $i=0; $i < $iNumberOfPics ; $i++ ) { | |
echo $aPics[0][$i]; | |
}; | |
}; | |
endwhile; | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment