How to get featured image of a product in woocommerce

前端 未结 8 1230
梦毁少年i
梦毁少年i 2021-02-05 00:26

Please tell me where I am going wrong . Product featured image is not showing up.

   $args = array( \'post_type\' => \'product\', \'posts_per_page\' => 80,         


        
相关标签:
8条回答
  • 2021-02-05 00:53

    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.

    0 讨论(0)
  • 2021-02-05 00:58

    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.

    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; ?>
    
    0 讨论(0)
提交回复
热议问题