php 如何获取 WordPress 帖子特色图片 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11261883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:07:34  来源:igfitidea点击:

How to get WordPress post featured image URL

phpwordpresspost

提问by ManpreetSandhu

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?

我怎样才能解决这个问题?

回答by swapnesh

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

回答by LOLapalooza

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

 

 

回答by Omprakash Patel

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

回答by Luis Felipe Barnett V

Easy way!

简单的方法!

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

回答by maxim

This perfectly worked for me:

这对我来说非常有效:

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

回答by Fredrick Boaz

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

我认为这是最简单的解决方案和更新的解决方案:

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

回答by Vishwajeet Mishra

This is the simplest answer:

这是最简单的答案:

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

回答by nim

You can try this:

你可以试试这个:

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

回答by yahya akhtar

Try this one

试试这个

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

回答by farhan

You can also get the URL for image attachments as follows. It works fine.

您还可以按如下方式获取图像附件的 URL。它工作正常。

if (has_post_thumbnail()) {
    $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'medium'); 
}