Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将URL查询参数添加到WP_查询_Php_Woocommerce_Wordpress - Fatal编程技术网

Php 将URL查询参数添加到WP_查询

Php 将URL查询参数添加到WP_查询,php,woocommerce,wordpress,Php,Woocommerce,Wordpress,我正在为我的WooCommerce商店实施产品过滤器。我想根据一些属性(如颜色)过滤产品,这些属性可以从URL查询参数中检索。例如,如果路径为/product category/clothing/?filter\u color=16,则仅显示颜色ID=16的产品 现在,当我从yithwoocommerce Ajax产品过滤器插件添加小部件时,这个功能似乎可用。但是,我不想使用这个插件,因为它与其他功能不一致,并且希望实现我自己的功能。但我不知道我是如何做到这一点的 我想让它同时适用于主循环和自定

我正在为我的WooCommerce商店实施产品过滤器。我想根据一些属性(如颜色)过滤产品,这些属性可以从URL查询参数中检索。例如,如果路径为
/product category/clothing/?filter\u color=16
,则仅显示颜色ID=16的产品

现在,当我从
yithwoocommerce Ajax产品过滤器
插件添加小部件时,这个功能似乎可用。但是,我不想使用这个插件,因为它与其他功能不一致,并且希望实现我自己的功能。但我不知道我是如何做到这一点的

我想让它同时适用于主循环和自定义循环。 通过主循环,我指的是:

<?php while ( have_posts() ) : the_post(); ?>
    <?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>

您可以检查查询,检索颜色值,检查它们是否匹配,如果匹配,然后显示产品。看看:

<?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. ?>


你知道WP对产品颜色的参数是什么吗?换句话说,在WooCommerce/WP中如何定义或检索产品的颜色?尝试使用钩子。@新浪我可以像这样访问颜色
get\u theu theu terms($product->ID,'pa\u color')
谢谢,它可以工作,但是在查询级别进行过滤会更有效吗?不用担心。我想这不会有太大的区别。尽管我建议您使用
,如果根本没有颜色过滤或查询的话。太棒了,享受编码吧:)
<?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. ?>