在 Woocommerce 的 WP_Query 循环中显示产品价格

Display the product price in a WP_Query loop in Woocommerce

我有这个代码来显示某个类别的产品,我还想显示它的价格。有什么我可以添加或更改的想法吗?下面的代码不显示任何内容(也没有错误)。

<?php

$product_categories = array('cat-name');

$wc_query = new WP_Query( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'tax_query' => array( array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => $product_categories,
        'operator' => 'IN',
    ) )
) );
?>
<h1 style="margin-top:30px;">Cat Name</h1>
<div class="changing-img">
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
<a href="<?php the_permalink(); ?>">
          <?php the_post_thumbnail('full'); ?>
          <?php the_post_thumbnail('full'); ?>
          <h6><?php the_title(); ?> </h6>
          <p><?php echo $wc_query->get_price_html(get_the_ID()); ?></p>
</a>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <li>
          <?php _e( 'No products' ); ?>
     </li>
     <?php endif; ?>
</div>

此外,如果可能的话,我想从 woocommerce 图库中提取第一张图片(不是缩略图)。非常感谢。

2020 年 8 月更新

您应该需要替换行:

<p><?php echo $wc_query->get_price_html(get_the_ID()); ?></p>

通过以下几行:

<?php $price = get_post_meta( get_the_ID(), '_price', true ); ?>
<p><?php echo wc_price( $price ); ?></p>

或者通过这种更好的方式(将输出正确格式的显示价格):

<?php $product = wc_get_product( get_the_ID() ); /* get the WC_Product Object */ ?>
<p><?php echo $product->get_price_html(); ?></p>