我正在尝试插入 WooCommerce 元素以将特定类别中的“产品”(课程)显示到页面中。
我还必须为这个特定类别隐藏这些产品,它按预期工作。我刚刚在functions.php中添加了一个过滤器,是这样的:
/*
* Exclude "packages" category from "Archive Products" on page 811
* Ref. URL: https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
*/
function exclude_category_archive_products( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'packages' ),
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
if( $current_post_id != "811" ) {
add_filter( 'woocommerce_product_query', 'exclude_category_archive_products' );
}
/* END Exclude "packages" category from "Archive Products" on page 811 */
我一直在寻找实现相反目标的方法,但“从今年或接近尾声”我没有找到任何东西。我尝试使用“IN”或“=”运算符,但它不起作用(它显示所有内容):
/*
* Display "packages" category only
*/
function show_only_category_in_page( $q ) {
var_dump("It reaches the function");
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'packages' ),
'operator' => '='
);
$q->set( 'tax_query', $tax_query );
}
if( $current_post_id == "811" ) {
var_dump("It reaches the page");
add_filter( 'woocommerce_product_query', 'show_only_category_in_page' );
}
/* END Display "packages" category only */
前面的代码string(23) "It gets reachs the page"
只写了。我究竟做错了什么?