Reputation: 101
HI I am using wordpress v3.03 and I am wondering how do I get featured images for the post in my theme
Currently I am using
get_the_post_thumbnail( $post->ID, 'post-thumbnail')
and it worked fine with 1 images, but when i kept add new images the above code always retrieve the first image i attached to the post instead of the image i set as "featured"
any help will be appreciated
Upvotes: 2
Views: 3516
Reputation: 6479
// Gets the post's featured image URL, if not available then use given fallback url.
function get_featured_image_url($fallback_url) {
if (is_front_page()) {
return $fallback_url;
}
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
if (!$thumb) {
return $fallback_url;
}
return $thumb[0];
}
Upvotes: 0
Reputation: 9703
In you're theme functions.php file add
add_theme_support( 'post-thumbnails' );
add_theme_support( 'post-thumbnails', array( 'post' ) );
Then in wp-admin when you edit a post you should have a featured image box where you can set 1 featured image . Then in the loop you can use the following functions :
the_post_thumbnail();
has_post_thumbnail();
Update
After you added post-thumbnails theme support , you need to set the featured image using the "featured image" box on the right sidebar on wp-admin/post.php page . It's right under the "post tags" box . Then outside the loop you can use get_the_post_thumbnail() function to retrive that image . If you didn't set any featured image for the post it will fetch the last image you inserted in that post .
One other way to set the featured image would be ( after you added post-thumbnail theme support ) , is when you insert an image into the post you will have a "set featured image" after you uploaded ( on the modal that let's you select image alignment ) . Then the image should be visible on the right sidebar "featured image" box .
Upvotes: 2