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

时间:2018-06-11 21:23:44

标签: php wordpress woocommerce product price

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

<?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图库(而不是缩略图)中提取第一张图片。非常感谢。

1 个答案:

答案 0 :(得分:4)

您需要更换一行:

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