要在WooCommerce中隐藏父类产品列表中的子类产品,您可以使用以下方法之一:
使用代码:
您可以通过添加一些自定义代码来实现此目标。首先,您需要在您的主题的functions.php文件中添加以下代码:
function exclude_child_categories_from_shop($query) {
if (is_admin() || !is_shop()) return $query;
$current_category = get_queried_object();
if ($current_category && $current_category>parent) {
$term_ids = array($current_category>term_id);
$child_terms = get_terms(array(
'taxonomy' => 'product_cat',
'child_of' => $current_category>term_id,
));
foreach ($child_terms as $child_term) {
$term_ids[] = $child_term>term_id;
}
$query>set('product_cat', implode(',', $term_ids));
}
return $query;
}
add_filter('pre_get_posts', 'exclude_child_categories_from_shop');
这段代码会检查当前页面是否为商店页面,并且如果是的话,它会排除子类别产品。您只需将此代码添加到您的主题的functions.php文件中,然后保存文件即可。
使用插件:
您还可以考虑使用WooCommerce插件,如"Hide Products by Category"或"Hide Categories on Shop Page"。这些插件可以让您在WordPress后台轻松设置产品隐藏规则,而无需编写任何代码。您可以在WordPress插件市场中搜索这些插件并安装它们。
请注意,插件的功能和界面可能会有所不同,具体取决于您选择的插件,因此您需要根据插件的文档或设置来配置它们以实现您的目标。
不管您选择哪种方法,都应该能够在WooCommerce中轻松隐藏子类别产品。