从Woocommerce中显示类型为子类别的产品类别中删除侧边栏

问题描述 投票:1回答:2

我想编写一个功能,从显示类型为子类别的woocommerce中的任何产品类别页面中删除侧边栏。

某种功能,如果此类别具有显示类型子类别,则会消失侧边栏。

任何帮助表示赞赏。

php wordpress woocommerce sidebar custom-taxonomy
2个回答
1
投票

它主要取决于您自己的主题自定义。所以以下代码只会处理:

  • 基于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文件中。经过测试和工作。


Then you will add one of those depending on your theme:

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_sidebar钩子的Storefront主题:

// 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)具有特定自定义的其他主题:

您将不得不找出用于使代码工作的钩子。


0
投票

默认情况下,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');

另一种选择是仅在显示类型为“子类别”时隐藏:

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');
© www.soinside.com 2019 - 2024. All rights reserved.