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

Display the First Image from Each of Your Posts

If your posts include an image that you would like to display as a thumbnail elsewhere on your blog, here is an easy way to do it with no custom fields required. Place the following code into your theme’s functions.php file:

function catch_that_image() {
	global $post, $posts;
	$first_img = '';
	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
	$first_img = $matches [1] [0];

		$first_img = "/images/default.jpg"; // default image
	return $first_img;

Then, to call the function and display the images as thumbnails, place the following code into the desired location within your loop:

<?php echo catch_that_image(); ?>

That’s all there is to it. If you would like to display a default image for posts that may not have one, specify its path in the code (on the line that says “default image).

Source: WPRecipes