我有一个很好的功能,排除在库存之外的产品类别。问题是我想要排除不止一个类别(实际上我只想在一个类别中显示,但我认为这就是方法。)如果有人只知道一个类别的表演.)。这是密码。谢谢!
/* Hyde out of stock product specific category */
add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' );
function hide_out_of_stock_from_cat( $query ) {
if ( $query->is_tax( 'product_cat', 15 ) && $query->is_main_query() ) {
$query->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
}发布于 2019-09-07 12:58:07
感谢@SallyCJ获得正确的答案--您可以向is_tax()提供一个类别ID/片段/名称的数组,以检查多个类别。以下是正确的代码:
/* Hyde out of stock product specific category */
add_filter( 'pre_get_posts', 'hide_out_of_stock_from_cat' );
function hide_out_of_stock_from_cat( $query ) {
if ( $query->is_tax( 'product_cat', array( 15, 16, 18, 19, 20, 21, 22, 23, 24, 59, 60, 62, 63, 66 ) ) && $query->is_main_query() ) {
$query->set( 'meta_query', array(array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT IN'
)));
}
}https://wordpress.stackexchange.com/questions/346809
复制相似问题