wordpress 如何在woocommerce中获得产品的特色图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32837968/
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
How to get featured image of a product in woocommerce
提问by Amar Singh
Please tell me where I am going wrong . Product featured image is not showing up.
请告诉我哪里出错了。产品特色图片未显示。
$args = array( 'post_type' => 'product', 'posts_per_page' => 80, 'product_cat' => 'profiler', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="dvThumb col-xs-4 col-sm-3 col-md-3 profiler-select profiler<?php echo the_title(); ?>" data-profile="<?php echo $loop->post->ID; ?>">
<img src="<?php get_the_post_thumbnail($loop->post->ID); ?>" data-id="<?php echo $loop->post->ID; ?>">
<p><?php the_title(); ?></p>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</div>
I have already added a featured image in back end
我已经在后端添加了特色图片
回答by Amar Singh
I got the solution . I tried this .
我得到了解决方案。我试过这个。
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $loop->post->ID ), 'single-post-thumbnail' );?>
<img src="<?php echo $image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">
回答by Amar Singh
The answers here, are way too complex. Here's something I've recently used:
这里的答案太复杂了。这是我最近使用的东西:
<?php global $product; ?>
<img src="<?php echo wp_get_attachment_url( $product->get_image_id() ); ?>" />
Using wp_get_attachment_url()
to display the
使用wp_get_attachment_url()
显示
回答by Alan
I would just use get_the_post_thumbnail_url()
instead of get_the_post_thumbnail()
我只是用get_the_post_thumbnail_url()
而不是get_the_post_thumbnail()
<img src="<?php echo get_the_post_thumbnail_url($loop->post->ID); ?>" class="img-responsive" alt=""/>
回答by Nishad Up
In WC 3.0+ versions the image can get by below code.
在 WC 3.0+ 版本中,可以通过以下代码获取图像。
$image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $item->get_product_id() ), 'single-post-thumbnail' );
echo $image_url[0]
回答by LIMEXS
get_the_post_thumbnail function returns html not url of featured image. You should use get_post_thumbnail_id to get post id of featured image and then use wp_get_attachment_image_src to get url of featured image.
get_the_post_thumbnail 函数返回 html 而不是特色图片的 url。您应该使用 get_post_thumbnail_id 获取特色图片的帖子 ID,然后使用 wp_get_attachment_image_src 获取特色图片的 url。
Try this:
尝试这个:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 80, 'product_cat' => 'profiler', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="dvThumb col-xs-4 col-sm-3 col-md-3 profiler-select profiler<?php echo the_title(); ?>" data-profile="<?php echo $loop->post->ID; ?>">
<?php $featured_image = wp_get_attachment_image_src( get_post_thumbnail_id($loop->post->ID)); ?>
<?php if($featured_image) { ?>
<img src="<?php $featured_image[0]; ?>" data-id="<?php echo $loop->post->ID; ?>">
<?php } ?>
<p><?php the_title(); ?></p>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</div>
<?php endwhile; ?>
回答by Clotilde Rodriguez
I did this and it works great
我这样做了,效果很好
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
<?php } ?>
回答by Nipuna
This should do the trick:
这应该可以解决问题:
<?php
$product_meta = get_post_meta($post_id);
echo wp_get_attachment_image( $product_meta['_thumbnail_id'][0], 'full' );
?>
You can change the parameters according to your needs.
您可以根据需要更改参数。