首页 > 解决方案 > 在 wordpress/woosommerce 中隐藏某些产品类别

问题描述

我试图从商店页面和其他页面上的类别小部件中隐藏某个子类别,有一个类别小部件。我发现下面的代码适用于除商店页面之外的所有页面。如果我更改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;
}

标签: wordpresswoocommerce

解决方案


只需删除您的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;
}

推荐阅读