How to get WordPress post featured image URL

PhpWordpressPost

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

 

Solution 3 - Php

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

Solution 4 - Php

Easy way!

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

Solution 5 - Php

This perfectly worked for me:

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

Solution 6 - Php

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

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

Solution 7 - Php

This is the simplest answer:

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

Solution 8 - Php

You can try this:

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

Solution 9 - Php

Try this one

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

Solution 10 - Php

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.

Solution 11 - Php

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

Solution 12 - Php

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

Solution 13 - Php

You can also get it from post_meta like this:

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

Solution 14 - Php

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

Solution 15 - Php

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

Solution 16 - Php

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

Solution 17 - Php

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

Solution 18 - Php

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.

Solution 19 - Php

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

Solution 20 - Php

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

Solution 21 - Php

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.

Solution 22 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionManpreetSandhuView Question on Stackoverflow
Solution 1 - PhpswapneshView Answer on Stackoverflow
Solution 2 - PhpLOLapaloozaView Answer on Stackoverflow
Solution 3 - PhpOmprakash PatelView Answer on Stackoverflow
Solution 4 - PhpLuis Felipe Barnett VView Answer on Stackoverflow
Solution 5 - PhpmaximView Answer on Stackoverflow
Solution 6 - PhpFredrick BoazView Answer on Stackoverflow
Solution 7 - PhpVishwajeet MishraView Answer on Stackoverflow
Solution 8 - PhpnimView Answer on Stackoverflow
Solution 9 - Phpyahya akhtarView Answer on Stackoverflow
Solution 10 - PhpJodyshopView Answer on Stackoverflow
Solution 11 - PhpfarhanView Answer on Stackoverflow
Solution 12 - PhpJakir HossainView Answer on Stackoverflow
Solution 13 - PhpDhyeyView Answer on Stackoverflow
Solution 14 - PhpViral MView Answer on Stackoverflow
Solution 15 - PhpDhruvin MoradiyaView Answer on Stackoverflow
Solution 16 - PhpHarry BoshView Answer on Stackoverflow
Solution 17 - Phpuser3615759View Answer on Stackoverflow
Solution 18 - PhpMohammad Farhan Atif WattooView Answer on Stackoverflow
Solution 19 - PhpJoyView Answer on Stackoverflow
Solution 20 - PhpVicky PView Answer on Stackoverflow
Solution 21 - PhpArman HView Answer on Stackoverflow
Solution 22 - PhpMizo GamesView Answer on Stackoverflow