In this tutorial, you’ll learn how to get the featured image URL in WordPress.
You can use the
wp_get_attachment_image_src function. It takes 3 parameters but we only need the first two:
<?php if (has_post_thumbnail()): ?> <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?> <img src="<?php echo $image; ?>" alt=""> <?php endif; ?>
In the example above we render a featured image if it exists. The size of the image is set to ‘large’ but you can also change it to ‘thumbnail’ (which is a default value) and ‘medium’.
If you find this post useful, please let me know in the comments below.
Want to share this with your friends?
PS: make sure you check other WordPress tutorials, e.g. how to speed up your WordPress site, why you should kill Disqus on your WordPress blog and download my plugin that improves your WordPress SEO & Performance.