2012-11-13 98 views
3

我一直試圖從SHOP頁面隱藏特定類別。我發現這個代碼:在WooCommerce商店頁面中隱藏類別

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文件,但我沒有實現的結果......

任何人可以幫助我嗎?

+1

請修正您帖子的標題。所有大寫字母只是簡單的粗魯.... – talonmies

回答

0

如果你想隱藏在你的主題某些類別你可以通過在wp_list_categories功能exclude參數:

wp_list_categories(array(
'taxonomy'    => 'product_cat', 
'hide_empty'   => 1, 
'use_desc_for_title' => 0, 
'title_li'    => ' ', 
'show_count'   => 0, 
'exclude'    => '63' // <-- Hidden)); 
6

我知道這是有點晚了,但有這個問題,我和同解決它以下功能:

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; 
} 
+1

我複製粘貼的代碼。添加類別slu。。刪除了'is_shop()'。但類別名稱仍然顯示在woocommerce的單個產品頁面上。 –

+0

你爲什麼刪除is_shop()? – danyo

+0

我只是不希望它出現在網站上的任何地方。不僅在商店頁面。這就是爲什麼。 –

0

下面的代碼片段正常工作對我來說: ADD_ACTION( 'pre_get_posts', 'custom_pre_get_posts_query');

功能custom_pre_get_posts_query($ Q){

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

if (! is_admin() && is_shop()) { 

    $q->set('tax_query', array(array(
     'taxonomy' => 'product_cat', 
     'field' => 'slug', 
     'terms' => array('your category slug'), // Don't display products in the knives category on the shop page 
     'operator' => 'NOT IN' 
    ))); 

} 

remove_action('pre_get_posts', 'custom_pre_get_posts_query'); 

}

我不知道我怎樣才能達到同樣的排除類別中的產品是通過產品搜索搜索,同時使用這些產品片段完全隱藏。

相關問題