在wordpress/ woosomcommerce中隐藏某些产品类别



我已经尝试从商店页面和其他页面上的类别小部件隐藏某个子类别,有一个类别小部件。我发现下面的代码适用于所有页面,除了商店页面。如果我将is_product_category()更改为is_shop(),则它适用于商店页面,但不适用于其他页面。

我怎样才能使它适用于所有页面(商店和所有其他页面)?

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
$new_terms = array();
// if a product category and on the shop page
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_product_category() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'seinakellad','nastennye-chasy','wall-clock' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}

只需删除if条件中的附加检查:

add_filter( 'get_terms', 'filter_get_terms', 10, 3 );
function filter_get_terms( $terms, $taxonomies, $args ) {
$new_terms = [];
// if a product category and on the shop page
if ( ! is_admin() ) {
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, [ 'seinakellad', 'nastennye-chasy', 'wall-clock' ] ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}

最新更新