0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Display All Images from Your Post Content

Want to display all of the images from each of your posts somewhere on your blog? Easy. Check out this code snippet from Matt Varone:

<?php if (have_posts()) : 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;
?>

Just customize the output in the second part of the code (see code comments) and paste the function wherever you would like to display the images. Sweetness.

Source: Matt Varone (404 link removed 2015/02/26)




0