Ree*_*ece 6 wordpress loops product woocommerce
我试图像使用自定义帖子类型一样遍历我的WooCommerce产品.但由于某种原因,这种方法不起作用.我收到的错误与我使用有关have_posts().我究竟做错了什么?
错误
未捕获错误:在数组上调用成员函数have_posts()
我的代码
<?php
 $query = new WC_Product_Query( array(
     'limit' => 10,
     'orderby' => 'date',
     'order' => 'DESC'
 ) );
 $products = $query->get_products();
 if( $products->have_posts() ) {
    while( $products->have_posts() ) {
      $products->the_post();
      echo the_permalink();
    }
} ?>
更新
我发现使用foreach循环确实如下所示;
<?php
foreach( $products as $product ) {
    echo $product->get_title();
} ?>
但是我仍然想知道为什么这种方法无效 have_posts()
$query = new WC_Product_Query(array(
    'limit' => 10,
    'orderby' => 'date',
    'order' => 'DESC'
        ));
$products = $query->get_products();
if (!empty($products)) {
    foreach ($products as $product) {
        echo get_permalink($product->get_id());
    }
}
函数have_post()是WordPress WP_Query类的成员函数-WooCommerce WC_Product_Query类是扩展WC_Object_Query类,而不是扩展类WP_Query-因此无法调用此函数