如何添加带有WooCommerce类别的筛选器?

时间:2020-10-06 作者:davidb3rn

我创建了一个显示产品的循环,但在尝试过滤产品时遇到了问题。我添加了tax\\u查询,因为这是使用分类法过滤搜索的方法(根据我的理解)。我已获得当前URL术语以进行筛选$term_search = get_queried_object()->slug; 我还进行了$term\\u搜索,以确保输出的信息正确。

如何使过滤器正常工作?值得一提的是,我已经更改了类别的永久链接,这会影响我的slug吗?

                $term_search = get_queried_object()->slug;
                // WP_Query arguments
                $args = array(
                    \'p\'                      => \'product\',
                    \'post_type\'              => array( \'product\' ),
                    \'order\'                  => \'ASC\',
                    \'post_per_page\' => 20,
                    \'tax_query\' => array(
                            \'taxonomy\' => \'product_cat\',
                            \'field\'    => \'slug\',
                            \'terms\' => $term_search, // (the name of what you want to filter by (latest or whatever))
                        \'include_children\' => true,
                        \'operator\' => \'IN\'
                        ),
                );

1 个回复
最合适的回答,由SO网友:davidb3rn 整理而成

我缺少tax\\u查询数组中数组的条件。无论您是否有一个或多个条件,都必须拥有它。

  $term_search = get_queried_object()->slug;
                // WP_Query arguments
                $args = array(
                    \'p\'                      => \'product\',
                    \'post_type\'              => array( \'product\' ),
                    \'order\'                  => \'ASC\',
                    \'post_per_page\' => 20,
                    \'tax_query\' => array(array(
                            \'taxonomy\' => \'product_cat\',
                            \'field\'    => \'slug\',
                            \'terms\' => $term_search, // (the name of what you want to filter by (latest or whatever))
                        \'include_children\' => true,
                        \'operator\' => \'IN\'
                        )),
                );

相关推荐

Loop through array of pages

我正在尝试递归地获取页面的所有子级(无限深度),我正在使用下面的函数,from this solution:function get_all_subpages($page, $args = \'\', $output = OBJECT) { if (! is_numeric($page)) $page = 0; $default_args = array( \'post_type\' => \'page\',&