3

SHOPページから特定のカテゴリを非表示にしようとしています。私はこのコードを見つけました:

add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;

    $q->set( 'tax_query', array(array(
        'taxonomy' => 'product_cat',
        'field' => 'slug',
        'terms' => array( 'CATEGORY TO HIDE' ),
        'operator' => 'NOT IN'
    )));

    remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );

}

このコードをテーマの function.php ファイルに貼り付けましたが、結果が得られません...

誰でも私を助けてもらえますか?

4

4 に答える 4

7

私はこれが少し遅れていることを知っていますが、この問題を自分で抱えていて、次の関数で解決しました:

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_shop() ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( '**CATEGORY-HERE**' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}
于 2013-10-23T21:54:23.730 に答える