wordpress - 如何显示特定子类别的产品?
问题描述
我想显示一个类别的特定子类别的产品列表。我的代码如下:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'Featured', 'orderby' => 'Desc', 'Parent' => 40);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<div class="col-md-12">
<div class="hmprdimgsmall">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" />'; ?>
<div class="hmprdname"><?php the_title(); ?></div>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
对于 'Parent' => 35 它有效。但是为 'Parent' 返回相同的结果 => 40 有什么建议吗?
解决方案
您需要为此创建一个新循环。这是我用于在主页上显示特定类别的产品的代码:
<ul class="products">
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<h2>Shoes</h2>
<li class="product">
<a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>
<h3><?php the_title(); ?></h3>
<span class="price"><?php echo $product->get_price_html(); ?></span>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
这是我的工作代码谢谢
推荐阅读
- python - 无法读取使用 soffice 命令转换 pdf 后得到的 .docx 文件
- python - Sqlalchemy:无法执行同步规则(在多对多关系中)
- arrays - 在 WindowsForm 中使用带有 DisplayArray 方法的泛型
- parse-error - ijson 因尾随垃圾解析错误而失败
- node.js - Node.js Socket.io 从不同的名称空间调用函数
- angular - 工具栏/离子按钮外的 Ionic 4 圆形图标仅按钮
- c - 在 C 中动态创建文件名
- reactjs - 带有反应的套接字 io:从服务器发射到特定用户
- javascript - 无法在本机反应中使用 AsyncStorage 保存用户名
- python - 用 Python 中另一个列表中的字符串替换列表中的数字