Post Pic

WordPress 2.9 : Display post image with backward compatibility

In a few days, WordPress 2.9 will be released. This all new version feature a bunch of interesting functions to display an image attached to a post.

Paste the following code, within the loop, where you want to post image to be displayed.

<?php
	if (  (function_exists('has_post_image')) && (has_post_image())  ) {
		the_post_image('thumbnail');
	} else {
		$postimageurl = get_post_meta($post->ID, 'post-image', true);
		if ($postimageurl) {
			echo '<img src="'.$postimageurl.'" alt="" />';
		}
	}
?>

What this code do :
-First, it makes sure that the has_post_image() function exists and that the current post have an image attached. If both conditions are true, the post image is displayed using WordPress 2.9 function the_post_image().
-If not, the get_post_meta() function is used to get the value of the custom field which contain the post image.

13 Responses

Nov 23 2009 12:53

Very useful, thanks a lot! =)

Nov 23 2009 16:08

great tip! Thanks!

Nov 23 2009 18:09

Uh oh… new version of wordpress.

Thanks, I use a lot of images on my sites so I might need this. I’ll have to see what damage WP 2.9 does first.

Nov 24 2009 00:53

Wow thats Great. Thanks for this recipes. On my next Blog in feautre i need it because i want to work with more images.

THX
Thomas

Nov 27 2009 02:24

Nice way of getting around any compatibility issues! :)

Dec 10 2009 02:15

Don’t you need to place something in functions.php to get the post image uploader to be displayed in the post panel?

Dec 12 2009 15:44

There has been a code change. You need to use the_post_thumbnail_*

Check dougal.gunters.org/blog/2009/12/11/post-thumbnails-changes

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required