我正在为我的 WooCommerce 商店实现产品过滤器。我想根据一些属性来过滤产品,例如颜色,可以从 URL 查询参数中检索。例如,如果路径是 /product-category/clothing/?filter_color=16
,那么只会显示颜色 ID = 16 的产品。
现在,当我从 YITH WooCommerce Ajax Product Filter
Plugin 添加小部件时,此功能似乎可用。但是,我不想使用这个插件,因为它与其他功能不一致,想实现我自己的。但我找不到 YITH 如何实现这一点。
我想让这个工作同时适用于主循环和我的自定义循环。
通过主循环,我指的是:
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
我的自定义循环:
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => $category->slug,
'orderby' => 'menu_order',
'order' => 'ASC'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) { ...
最佳答案
您可以检查查询,检索颜色的值,检查它们是否匹配,如果匹配则显示产品。看一下:
<?php while ( have_posts() ) : the_post(); ?>
<?php
if(isset($_GET['filter_color']) //check if the filter color is set
{
$color=$_GET['filter_color'];
$productColor = get_the_terms($product->ID,'pa_color');
if ($color == $productColor) //if the filter color matches with the color of the prodct
wc_get_template_part( 'content', 'product' ); //then show the product
}
?>
<?php endwhile; // end of the loop. ?>
同样的方法适用于您的自定义循环。
关于php - 向 WP_Query 添加 URL 查询参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32857289/