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).
1 2 3 4 5 |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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