Get featured image URL in WordPress

in Code Write a comment

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: $attachment_id and $image_size.

<?php if (has_post_thumbnail()): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' ); ?>
  <img src="<?php echo $image[0]; ?>" 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’.

Hi, I'm Renat 👋


If you find this post useful, please let me know in the comments below. 
Renat Galyamov

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.

A collection of UI components for
Bootstrap 4/5 and Vue.js

Get now

Write a Comment