php - How to get the WordPress post thumbnail (featured image) URL?

767

I am using this function to get the featured images:

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

Now I want to get the full featured image on click on the anchor tag for which I need a featured image URL in

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

How can I fix this?

696

Answer

Solution:

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" class="page_speed_485499490">

  </div>
<?php endif; ?>
788

Answer

Solution:

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 ?>" />

 

524

Answer

Solution:

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

Answer

Solution:

Easy way!

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

Answer

Solution:

This perfectly worked for me:

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

Answer

Solution:

I think this is the easiest solution and the updated one:

<?php the_post_thumbnail('single-post-thumbnail'); ?>
86

Answer

Solution:

This is the simplest answer:

<?php
    $img = get_the_post_thumbnail_url($postID, 'post-thumbnail');
?>
865

Answer

Solution:

You can try this:

<?php 
    $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
    echo $feat_image; 
?>
983

Answer

Solution:

Try this one

<?php 
    echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); 
?>
306

Answer

Solution:

I had searched a lot and found nothing, until I got this:

<?php echo get_the_post_thumbnail_url( null, 'full' ); ?>

Which simply give you the full image URL without the entire<img> tag.

Hope that can help you.

105

Answer

Solution:

You can try this.

<?php
   $image_url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
?>
<a href="<?php echo $image_url; ?>" rel="prettyPhoto">
809

Answer

Solution:

You can also get it from post_meta like this:

echo get_post_meta($post->ID, 'featured_image', true);
987

Answer

Solution:

You can also get the URL for image attachments as follows. It works fine.

if (has_post_thumbnail()) {
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
}
207

Answer

Solution:

You will try this

<?php $url = wp_get_attachment_url(get_post_thumbnail_id($post->ID), 'full'); ?> // Here you can manage your image size like medium, thumbnail, or custom size
    <img src="<?php echo $url ?>" 
/>
123

Answer

Solution:

<?php
    if (has_post_thumbnail( $post->ID ) ):
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
        <img src="<?php echo $image[0]; ?>">  
<?php endif; ?>
434

Answer

Solution:

If the post is an image and we already know what the image is, it's possible to get the thumbnail URL without too much hassle:

echo pathinfo($image->guid, PATHINFO_DIRNAME);
105

Answer

Solution:

Simply inside the loop write<?php the_post_thumbnail_url(); ?> as shown below:-

$args=array('post_type' => 'your_custom_post_type_slug','order' => 'DESC','posts_per_page'=> -1) ;
$the_qyery= new WP_Query($args);

if ($the_qyery->have_posts()) :
  while ( $the_qyery->have_posts() ) : $the_qyery->the_post();?>

<div >
  <div >
    <article >
      <div >
        <a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="Post"></a>
      </div>

    </article>
  </div>
</div>      
<?php endwhile; endif; ?>
526

Answer

Solution:

Use:

<?php 
    $image_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'thumbnail_size');

    $feature_image_url = $image_src[0]; 
?>

You can change thethumbnail_size value as per your required size.

963

Answer

Solution:

You can also get the URL for image attachments as follows:

<?php
    "<div><a href=".get_permalink(id).">".wp_get_attachment_url(304, array(50,50), 1)."</a></div>";
?>
256

Answer

Solution:

You can get image src for specific size bywp_get_attachment_url function.

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

Answer

Solution:

if you want to fetch full image size from post thumbnail you can use this code.

$img_url = wp_get_attachment_image_url(get_post_thumbnail_id(get_the_ID()), 'full');
<img src="<?PHP echo $img_url?> ">

here, get_the_ID() is post id.

63

Answer

Solution:

<img src="<?php echo get_post_meta($post->ID, "mabp_thumbnail_url", true); ?>" alt="<?php the_title(); ?>" width ="100%" height ="" />

People are also looking for solutions to the problem: PHP JQuery Ajax change button val during processing FTP download

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.