我有以下代码从 WooCommerce 产品类别小部件中为除管理员和批发客户(自定义用户级别)之外的所有用户删除“批发类别”(通过 slug)。
这很好用,但是它只会删除父类别并保留所有子类别。如何删除“批发类别”的子类别?
// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
$category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'wholesale-category' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
谢谢
尝试这样的事情......
// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );
if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {
foreach ( $terms as $key => $term ) {
if ( in_array( $term->slug, array( $category_to_check->slug ) ) || ( $category_to_check->term_id == $term->parent ) ) {
unset($terms[$key]);
}
}
}
return $terms;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句