Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get WordPress post featured image URL

People also ask

How do I find the featured image URL in WordPress?

If you're using WordPress 4.4+ (released in 2015) you can use the get_the_post_thumbnail_url() function to return the URL of the featured post image. This is useful if you want to use the featured image URL in a background-image style or making a unique theme element that specifically needs the featured image URL.

Where are WordPress featured images stored?

The post featured image link stored in the WordPress database is stored in wp_postmeta with a meta_key called _thumbnail_id. And, the actual thumbnail link is then contained in wp_posts with a post_type of attachment.

Can you add a link to a featured image in WordPress?

On WordPress.com it's not possible to add a link directly to a featured image, but featured images should by default link to the post where they appear in the main feed of the site.


Check the code below and let me know if it works for you.

<?php if (has_post_thumbnail( $post->ID ) ): ?>
  <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
  <div id="custom-bg" style="background-image: url('<?php echo $image[0]; ?>')">

  </div>
<?php endif; ?>

If you want JUST the source, and not an array with other information:

<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" />

 


// Try it inside loop.  
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo $feat_image;
?>

Easy way!

 <?php 
     wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()))
 ?>

This perfectly worked for me:

<?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?>