Coder Perfect

How do I get the URL for the featured image in a WordPress post?

Problem

This function is what I’m using to obtain the featured images:

<a href="#" rel="prettyPhoto">
    <?php the_post_thumbnail('thumbnail'); ?>
</a>

Now I need a featured image URL in order to obtain the complete featured image when I click on the anchor tag.

<a href="here" rel="prettyPhoto">

What can I do about it?

Asked by ManpreetSandhu

Solution #1

Please test the code below and let me know if it works.

<?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; ?>

Answered by swapnesh

Solution #2

If you only want the source and not an array of extra data, type:

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

Answered by LOLapalooza

Solution #3

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

Answered by Omprakash Patel

Solution #4

Easy way!

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

Answered by Luis Felipe Barnett V

Solution #5

This worked well for me:

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

Answered by maxim

Post is based on https://stackoverflow.com/questions/11261883/how-to-get-wordpress-post-featured-image-url