How to get WordPress post featured image url

2019-01-10 02:43发布

问题:

I am using a 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 anchor tag for which i need a featured image url in

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

please help

回答1:

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


回答2:

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:

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


回答4:

Easy way!

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


回答5:

This perfectly worked for me:

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


回答6:

hi i think this is the most easiest solution and the updated one;

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


回答7:

You can try this:

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


回答8:

This is the most simplest answer. <?php $img = get_the_post_thumbnail_url($postID,'post-thumbnail' ); ?>



回答9:

Try this one

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



回答10:

You can also use for getting 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' ); 
}


回答11:

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


回答12:

You can also get it from post_meta like this:

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


回答13:

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


回答14:

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


回答15:

you can also use for getting 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>";
?>


回答16:

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



回答17:

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


回答18:

 <?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 thumbnail_size value as per your require size.