在WooCommerce商店页面中隐藏类别
我一直试图从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文件,但我没有实现的结果......
任何人可以帮助我吗?
如果你想隐藏在你的主题某些类别你可以通过在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));
我知道这是有点晚了,但有这个问题,我和同解决它以下功能:
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;
}
我复制粘贴的代码。添加类别slu。。删除了'is_shop()'。但类别名称仍然显示在woocommerce的单个产品页面上。 –
你为什么删除is_shop()? – danyo
我只是不希望它出现在网站上的任何地方。不仅在商店页面。这就是为什么。 –
下面的代码片段正常工作对我来说: 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');
}
我不知道我怎样才能达到同样的排除类别中的产品是通过产品搜索搜索,同时使用这些产品片段完全隐藏。
请修正您帖子的标题。所有大写字母只是简单的粗鲁.... – talonmies