从产品类别中删除边栏,显示类型是 Woocommerce 中的子类别
Remove sidebar from product categories which display type is subcategories in Woocommerce
我想编写一个函数,从显示类型为子类别的 woocommerce 中的任何产品类别页面中删除边栏。
某种功能表明如果该类别具有显示类型子类别则侧边栏消失。
感谢任何帮助。
主要看你的主题自己定制。所以下面的代码只会处理:
- 基于
woocommerce_sidebar
操作挂钩的默认 woocommerce 行为。
- 基于storefront_sidebar动作挂钩的店面主题。
自定义条件函数:
首先,下面是一个自定义条件函数,它将检查产品类别术语是否设置为 'subcategories'
显示类型:
// Custom conditional function that check for "subcategories" display type in product categories term
function is_subcategory_display_type( $term ) {
$taxonomy = 'product_cat';
if( ! term_exists( $term, $taxonomy ) )
return false;
if( ! is_numeric( $term ) )
$term = get_term_by( 'slug', sanitize_title( $term ), $taxonomy )->term_id;
return get_term_meta( $term, 'display_type', true ) === 'subcategories' ? true : false;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
然后您将根据您的主题添加其中之一:
1) 对于使用默认 woocommerce_sidebar
挂钩的普通主题:
// Removing default themes woocommerce sidebar conditionally
add_action( 'woocommerce_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('woocommerce_sidebar','woocommerce_get_sidebar', 10 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
2) 对于 Storefront 主题 使用它自己的 storefront_sidebar
挂钩:
// Removing default "Storefront" theme woocommerce sidebar conditionally
add_action( 'storefront_sidebar', 'remove_storefront_get_sidebar', 1, 1 );
function remove_storefront_get_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
3) 具有特定定制的其他主题:
You will have to find out which hook is used to make the code work.
默认情况下,woocommerce 使用默认类型(如果存在则显示子类别,如果没有子类别则显示产品)
要检查当前值,请使用条件函数:
woocommerce_products_will_display()
因此您可以像这样删除侧边栏:
function remove_storefront_sidebar( $name ){
if ( is_product_category() && !woocommerce_products_will_display() ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');
另一种选择是仅当显示类型为"subcategories":
时才隐藏
function remove_storefront_sidebar( $name ){
$display_type = woocommerce_get_loop_display_mode();
if ( is_product_category() && 'subcategories' === $display_type ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');
我想编写一个函数,从显示类型为子类别的 woocommerce 中的任何产品类别页面中删除边栏。
某种功能表明如果该类别具有显示类型子类别则侧边栏消失。
感谢任何帮助。
主要看你的主题自己定制。所以下面的代码只会处理:
- 基于
woocommerce_sidebar
操作挂钩的默认 woocommerce 行为。 - 基于storefront_sidebar动作挂钩的店面主题。
自定义条件函数:
首先,下面是一个自定义条件函数,它将检查产品类别术语是否设置为 'subcategories'
显示类型:
// Custom conditional function that check for "subcategories" display type in product categories term
function is_subcategory_display_type( $term ) {
$taxonomy = 'product_cat';
if( ! term_exists( $term, $taxonomy ) )
return false;
if( ! is_numeric( $term ) )
$term = get_term_by( 'slug', sanitize_title( $term ), $taxonomy )->term_id;
return get_term_meta( $term, 'display_type', true ) === 'subcategories' ? true : false;
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
然后您将根据您的主题添加其中之一:
1) 对于使用默认 woocommerce_sidebar
挂钩的普通主题:
// Removing default themes woocommerce sidebar conditionally
add_action( 'woocommerce_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('woocommerce_sidebar','woocommerce_get_sidebar', 10 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
2) 对于 Storefront 主题 使用它自己的 storefront_sidebar
挂钩:
// Removing default "Storefront" theme woocommerce sidebar conditionally
add_action( 'storefront_sidebar', 'remove_storefront_get_sidebar', 1, 1 );
function remove_storefront_get_sidebar( $name ){
$queried_object_id = get_queried_object_id();
if ( is_product_category() && is_subcategory_display_type( $queried_object_id ) ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
3) 具有特定定制的其他主题:
You will have to find out which hook is used to make the code work.
默认情况下,woocommerce 使用默认类型(如果存在则显示子类别,如果没有子类别则显示产品) 要检查当前值,请使用条件函数:
woocommerce_products_will_display()
因此您可以像这样删除侧边栏:
function remove_storefront_sidebar( $name ){
if ( is_product_category() && !woocommerce_products_will_display() ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');
另一种选择是仅当显示类型为"subcategories":
时才隐藏function remove_storefront_sidebar( $name ){
$display_type = woocommerce_get_loop_display_mode();
if ( is_product_category() && 'subcategories' === $display_type ){
remove_action('storefront_sidebar','storefront_get_sidebar', 10 );
}
}
add_action( 'storefront_sidebar', 'remove_storefront_sidebar');