171

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?

22 Answers 22

363

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; ?>
5
  • 1
    thank u very much it is work . i want to add also condition. like if have post thumbail then show this and if not then another div or css or default image any idea how
    – pagol
    Aug 2, 2015 at 3:57
  • 2
    i made it but it is correct way or not i don't know. <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?> <div class="section bannerarea cashstudybanner" style="background-image: url( <?php if ( has_post_thumbnail() ) { echo $image[0]; } else { ?> <?php bloginfo('template_directory'); ?>/images/common-banner.jpg <?php } ?> )">
    – pagol
    Aug 2, 2015 at 4:35
  • Thank you very much! This code is working perfectly. Jul 21, 2018 at 6:07
  • 2
    Just thought I'd drop by to say I use this answer all the time. It's downright comical that almost a full decade after this post wordpress still hasn't simplified this dramatically.
    – Kai Qing
    Mar 11, 2021 at 21:53
  • Thank you a lot. This code works with the user logged, but the user not logged in the image is null. Any suggestion for my problem. I using WP 5.7
    – Trung
    Jul 6, 2021 at 2:47
117

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

 

3
  • 3
    For version 4.4.0 and up wp_get_attachment_image_url(). Sep 29, 2016 at 11:50
  • 1
    this was exactly what I needed. loads full img src. thank you! Nov 2, 2016 at 21:31
  • 2
    wp_get_attachment_image_url is only applicable if you know the attachment ID (not post ID). Nov 22, 2016 at 15:20
23
// Try it inside loop.  
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
echo $feat_image;
?>
1
  • 4
    @gavard.e would actually work with " instead of ' :P But the quotes are not necessary at all. Feb 11, 2016 at 11:56
19

Easy way!

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

This perfectly worked for me:

<?php echo get_the_post_thumbnail_url($post_id, 'thumbnail'); ?>
2
  • 4
    Note: Only available since Wordpress 4.4.0
    – MarcGuay
    Jul 11, 2016 at 19:15
  • <?php echo get_the_post_thumbnail_url($post_id); ?> If you want the full size image.
    – yeahlad
    Sep 20, 2019 at 7:14
10

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

<?php the_post_thumbnail('single-post-thumbnail'); ?>
1
  • The one that is at the top didn't work (without PHP errors, which I didn't want as I'm building a WordPress theme for the masses) but this one did. +1 Sep 26, 2018 at 16:37
8

This is the simplest answer:

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

You can try this:

<?php 
    $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID)); 
    echo $feat_image; 
?>
2
  • this only print: '$feat_image'; May 11, 2015 at 12:43
  • my mistake its, echo $feat_image;
    – nim
    Jun 16, 2015 at 7:16
6

Try this one

<?php 
    echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); 
?>
1
  • 1
    Use get_the_post_thumbnail_url if you want the image URL only.
    – Gavin
    Sep 23, 2019 at 8:35
5

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.

3

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">
3

You can also get it from post_meta like this:

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

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'); 
}
3

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

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.

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

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);
1

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 class="col col_4_of_12">
    <div class="article_standard_view">
        <article class="item">
            <div class="item_header">
                <a href="<?php the_permalink(); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="Post"></a>
            </div>

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

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 the thumbnail_size value as per your required size.

0
0

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

You can get image src for specific size by wp_get_attachment_url function.

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.