首页 > 解决方案 > 在一行中按类别获取并显示有限的无特色产品

问题描述

我有一个 woo commerce wordpress 模板,我有不同类别的特色产品。获取产品并在首页上显示它们很好,但问题是当我按类别调用产品时,所有产品都显示在该类别的一行中,但我只想显示有限的产品,因为我是 PHP 新手,我不知道如何控制循环这里是代码:

<section class="products">
  <div class="container">
    <h2 class="h2 upp align-center"> Hybrid devices</h2>
    <hr class="offset-lg">

    <div class="row">

        <?php $query_args = array (
        
        'category' => array('hybrid-devices'),
    );
    $products = wc_get_products($query_args) ;
    global $post;
    $columns = wc_get_loop_prop('columns');
        ?>
        <div class="woocommerce columns-<?php echo esc_attr( $columns ); ?>">
<?php
woocommerce_product_loop_start();
foreach ($products as $product) {
    $post = get_post($product->get_id());
    setup_postdata($post);
    wc_get_template_part('content', 'product');
}
wp_reset_postdata();
woocommerce_product_loop_end();
?>
</div>

           
</div>
    <div class="align-right align-center-xs">
      <hr class="offset-sm">
      <a href="./store/"> <h5 class="upp">View all devices </h5> </a>
    </div>
  </div>
</section>

标签: phphtmlwordpress

解决方案


如果我理解正确,您只需要 X 数量的产品,它们不应该被推荐。

如果是这样,我认为这个查询应该有效:

query_args = array (
    'category' => array('hybrid-devices'),
    'limit' => 6, // ... or how many you want
    'featured' => false, // Exclude featured products
);

查看 WooCommerce wiki 中的此页面以获取有关 WooCommerce 产品查询的更多信息:wc_get_products 和 WC_Product_Query


推荐阅读