How to get WordPress post featured image url

2019-01-10 02:07发布

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

18条回答
时光不老,我们不散
2楼-- · 2019-01-10 02:46

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; ?>
查看更多
Explosion°爆炸
3楼-- · 2019-01-10 02:48
<?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; ?>
查看更多
姐就是有狂的资本
4楼-- · 2019-01-10 02:49

Try this one

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

查看更多
smile是对你的礼貌
5楼-- · 2019-01-10 02:55
 <?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.

查看更多
Melony?
6楼-- · 2019-01-10 02:56

You can also get it from post_meta like this:

echo get_post_meta($post->ID, 'featured_image', true);
查看更多
再贱就再见
7楼-- · 2019-01-10 03:00

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' ); 
}
查看更多
登录 后发表回答