The WordPress Featured Image is a great tool where ones pages and posts can be easily illustrated with a pre-configured display of any selected image that has been uploaded to the WordPress Media Library. As a WordPress theme developer, I often have to find new ways to display the featured image, or in the case of editing somebody else s WordPress theme, I may find myself using the feature image function in combination with other display tools, such as timthumb.php or ???
My preferred method is to grab the url of the featured image, then use that to construct what will be needed given the design brief. The following is my code to grab the featured image URL:
Display URL Example
<?php if (has_post_thumbnail( $post->ID )) : ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <img src="<?php echo $image; ?>" /> <?php endif; ?>
Easy peasy. How about getting it to display as an image? Note I’ve added an image class to help me style how the image will be displaying.
Image Display Example
<?php if (has_post_thumbnail( $post->ID )) : ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <img class="myFeatured-image" src="<?php echo $image; ?>" />" alt="Featured Image" /> <?php endif; ?>
Let us link that displayed image to something. In this example I’m showing the featured image in a lightbox effect:
Hyperlinked Example
<?php if (has_post_thumbnail( $post->ID )) : ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <a href="<?php echo $image; ?>" title="Featured Image" rel="fancybox"><img class="myFeatured-image" src="<?php echo $image; ?>" />" alt="Featured Image" /></a> <?php endif; ?>
Showing the featured image full size then linking the same image to appear in a lightbox effect is silly at best. What generally occurs is displaying an image thumbnail, then having that link to the full size image. To achieve this I use the popular (and now secure) timthumb.php script found at Google
TimThumb.php Example
<?php if (has_post_thumbnail( $post->ID )) : ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <a href="<?php echo $image; ?>" title="Featured Image" rel="fancybox"> <img class="myFeatured-image" src="http://your.path.to.timthumb.php?src=<?php echo $image; ?>&q=100" />" alt="Featured Image" /> </a> <?php endif; ?>
Notes
In the above example, I’ve added q=100 which sets the rendered jpg quality to 100%. There are more variables that can be added, including how and where to crop. You will need the GD library installed on your server (who doesn’t these days). Learn more about the timthumb script and its primary developer Ben Gilbanks from Ben’s TimThumb website
Stay abreast of new and or updated items by opting in to my very occasional email newsletter. I swear by all that is right and holy your email address will never be shared, sold, traded or otherwise abused.
[wysija_form id=”1″]
Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Ie. I’m not sure if this is a format issue or something to do with web browser compatibility but I figured I’d post to let you know. The design look great though! Hope you get the problem fixed soon. Thanks
Hey, you caught me with my theme down. C’mon back and check again.
You are very kind. I try to resist that temptation (its the Devil), like the time the car wash ripped off my antenna, ARGH!
Gawd?! MSIE still sucking…
How to show featured image in post background? I have seen the code here https://www.wpblog.com/wordpress-get-featured-image-url/ but having some issues to implement it as I have added this code in template.php but the image is not showing in background
$blog_fetrdimg = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<div class="blog-featured-image"
style=”blog-featured-background-image: url();”
That code should work. Where are you trying to implement this? URL? And why template.php? Are you using a child theme? I think I would try testing using single.php or page.php and creating a new post or page for testing. Better yet, copy single.php as single-featured.php then assign that to the new post and test. Would be a better idea if it is a live site.