[php] How to get WordPress post featured image URL

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?

This question is related to php wordpress post

The answer is


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

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

You can also get it from post_meta like this:

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

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

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

You can try this:

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

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

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

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

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

This perfectly worked for me:

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

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.


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

Easy way!

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

This is the simplest answer:

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

Try this one

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

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

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

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.


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

 


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to wordpress

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’ How to get WooCommerce order details Wordpress plugin install: Could not create directory WooCommerce: Finding the products in database How to get post slug from post in WordPress? How to get featured image of a product in woocommerce Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\wordpress\wp-includes\class-http.php on line 1610 Use .htaccess to redirect HTTP to HTTPs Load More Posts Ajax Button in WordPress How to decode encrypted wordpress admin password?

Examples related to post

How to post query parameters with Axios? How can I add raw data body to an axios request? HTTP POST with Json on Body - Flutter/Dart How do I POST XML data to a webservice with Postman? How to set header and options in axios? Redirecting to a page after submitting form in HTML How to post raw body data with curl? How do I make a https post in Node Js without any third party module? How to convert an object to JSON correctly in Angular 2 with TypeScript Postman: How to make multiple requests at the same time