簡體   English   中英

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM