【问题标题】:Remove sidebar from product categories which display type is subcategories in Woocommerce从显示类型为 Woocommerce 子类别的产品类别中删除侧边栏
【发布时间】:2018-10-13 05:00:27
【问题描述】:

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

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

感谢任何帮助。

【问题讨论】:

  • 你有什么扁平化主题的解决方案吗?

标签: php wordpress woocommerce sidebar custom-taxonomy


【解决方案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 文件中。经过测试并且可以工作。


然后您将根据您的主题添加其中之一:

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

您必须找出使代码工作的钩子。

【讨论】:

  • 非常感谢!它确实有效!只有一个小问题......主要内容仍然认为那里有一个侧边栏,所以 css 正在使它更小。有没有办法,除了像我一样弯曲它,让它全宽?
  • 我想我是自己想出来的......在stackoverflow的帮助下!这是我为您的主题功能制作的模组。你怎么看?有没有更好的办法?我将此添加到您主题函数中的 if `?>
  • 不确定如何格式化这些 cmets 很抱歉有点模糊...我试过了 :(
  • 你有什么扁平化主题的解决方案吗?
【解决方案2】:

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

【讨论】:

  • 你有什么扁平化主题的解决方案吗?
猜你喜欢
  • 2015-11-16
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 2018-05-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多