2015-09-06 82 views
1

我正在使用內置的woocommerce類別小部件,此刻它同時顯示類別和子類別。隱藏woocommerce類別小部件的子類別

我排除通過這個代碼類別:

add_filter('woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category'); 
function organicweb_exclude_widget_category($args) { 
// Enter the id of the category you want to exclude in place of '30' 
    $args['exclude'] = array('62'); 
    return $args; 
} 

,但小部件仍顯示它的子類。

鏈接:http://tithaty.com.br/?post_type=product

類別隱藏的是Coleções(我設置爲父母),我想隱藏它的子類別,當前和今後加入的影片。

Colecao teste是一個子類別的例子。

任何想法?

謝謝

回答

1

您需要修改過濾器代碼。我在代碼中添加了註釋以幫助您瞭解它的工作原理。代碼將確保Coleções的現有子類和將來添加的子類始終隱藏。

add_filter('woocommerce_product_categories_widget_args', 'organicweb_exclude_widget_category'); 

function organicweb_exclude_widget_category($args) { 

    // Create an array that will hold the ids that need to be excluded 
    $exclude_terms = array(); 

    // Push the default term that you need to hide 
    array_push($exclude_terms, 62); 

    // Find all the children of that term 
    $termchildren = get_term_children(62, 'product_cat'); 

    // Iterate over the terms found and add it to the array which holds the IDs to exclude 
    foreach($termchildren as $child) { 
     $term = get_term_by('id', $child, 'product_cat');  
     array_push($exclude_terms, $term->term_id); 
    } 

    // Finally pass the array 
    $args['exclude'] = $exclude_terms; 

    return $args; 
} 
相關問題