php - 在一行中按类别获取并显示有限的无特色产品
问题描述
我有一个 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>
解决方案
如果我理解正确,您只需要 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
推荐阅读
- regex - 从列中删除最后一个字符
- mysql - Mysql key:是否保证单键比复合键快?
- github-actions - 由于缺少库,Github Action 在 Windows 上失败
- vue.js - Vue JS:将数据从本地 js 文件中拉到下拉列表中
- json - (非Json数据+Json数据)转Json数据
- javascript - Express & Node JS - 使用 findOne 或 findById 时未定义
- javascript - 工具提示正在与父级断开
- laravel - Laravel 顶级销售产品功能
- java - 为什么加入小缓冲图像的 Java 代码不起作用?
- reactjs - 为什么 React 将其错误引用到我的包而不是源映射(Webpack 5.44.8)?