Categories

Associating images to a post or a page

In the version 2.9 WordPress introduced the capability of associating images to a post or a page. When you create your theme you can turn on this feature by simply adding a call to add_theme_support('post-thumbnails') in your functions.php (this file should be located inside you theme folder).

if ( function_exists( 'add_theme_support') ) {
	add_theme_support( 'post-thumbnails' );
	add_image_size( 'image-normal', 96, 9999, false );
	add_image_size( 'image-small', 32, 9999, false );
}

Believe it or not you are ready to go. Once you add this feature, the next time that you are creating or editing a post in the WordPress admin area, you will be presented with the option to add a Feature Image to your post. This will allow you to upload and associate an image to your post.

To show the image in your page you can simply call the_post_thumbnail($feature). Here is an example:

<?php 
	$posts = get_posts();
	foreach ($posts as $post){
		setup_postdata($post); 
		if(is_search()){
			?>
			<p id="<?=the_ID();?>"><?=the_title();?></p>
			<?php the_post_thumbnail('image-small');
		}else{
			?>
			<h1 id="<?=the_ID();?>"><?=the_title();?></h1>
			<?php the_post_thumbnail('image-normal');
		}
	}
?>

References

Add Theme Support
Add Image Size
The Post Thumbnail

Related articles

Retrieving The Thumbnail Image

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

IMPORTANT! To be able to proceed, you need to solve the following simple math (so I know that you are a human) :-)

What is 4 + 2 ?
Please leave these two fields as-is: