从产品类别页面中删除侧边栏 | WordPress Woocommerce

Joh*_*nky 0 css php wordpress woocommerce

我的问题是,如何仅删除特定产品类别“Slug”的侧边栏,而不删除其子 slug。
如果 url 如下所示 - 删除侧栏并使页面全宽,仅用于 slugs“sanitaryware”和“closets” http://www.example.com/product-category/sanitaryware/
http://www.example .com/product-category/sanitaryware/closets/

我不想因为所有“产品类别”原因删除侧边栏,我希望侧边栏显示孙子蛞蝓“one-piece-closets”:http :
//www.example.com/product-category/卫生洁具/壁橱/一件式壁橱

代码:我在 function.php 中使用的 - 这是删除网站所有产品类别中的侧栏。

  <?php
            /**
             * woocommerce_sidebar hook
             *
             * @hooked woocommerce_get_sidebar - 10
             */
            if (!is_product_category('sanitaryware')){
        do_action('storefront_sidebar');
}
?>
Run Code Online (Sandbox Code Playgroud)

hel*_*ing 5

根据您希望隐藏顶级类别及其直接子项的侧边栏,我们将需要一个系统来确定任何术语档案的分层“深度”。类似于人们经常获得“顶级”父术语的方式,我们可以while循环获取术语的术语对象并检查术语的父术语。在我们的例子中,我们不会返回顶级父级,而是保持一个计数并返回它。

/**
* Returns depth level of product category
*
* @param    string      $catid  Product category ID to be checked
* @return   string      $depth  how many categories deep is this term in the hierarchy
*/
function so_32165017_get_product_cat_depth($catid) {
    $depth = 0;
    while ($catid) {
        $cat = get_term_by('id', $catid, 'product_cat'); // get the object for the catid
        if( $cat->parent > 0 ){
            $depth++;
        }
        $catid = $cat->parent; // assign parent ID (if exists) to $catid
        // the while loop will continue whilst there is a $catid
    }
    return $depth;
}
Run Code Online (Sandbox Code Playgroud)

现在我们有了可以用作条件的东西,我们可以有条件地删除 WooCommerce 侧边栏:

/**
* Hide the sidebar for items 2 levels deep or more
*/
function so_32165017_conditionally_remove_sidebar(){
    if( is_product_category()){
        $t_id = get_queried_object()->term_id;
        if( so_32165017_get_product_cat_depth( $t_id ) < 2 ){
            remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
            // could be theme specific ex: Storefront
            remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
        }
    }
}
add_action( 'woocommerce_before_main_content', 'so_32165017_conditionally_remove_sidebar' );
Run Code Online (Sandbox Code Playgroud)

编辑/更新

如果您还想添加自定义正文类以使无侧边栏的页面更易于设置样式,那么我相信您可以在body_class实际过滤正文类的同时从过滤器中删除有问题的操作。

/**
* Hide the sidebar for items 2 levels deep or more
*/
function so_32165017_conditionally_remove_sidebar( $class ){
    if( function_exists('is_product_category') && is_product_category() ){
        $t_id = get_queried_object()->term_id;
        if( so_32165017_get_product_cat_depth( $t_id ) < 2 ){
            remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
            // could be theme specific ex: Storefront
            remove_action( 'storefront_sidebar', 'storefront_get_sidebar', 10 );
            // add a custom body class
            $class[] = 'full-width';
        }
    }
    return $class;
}
add_action( 'body_class', 'so_32165017_conditionally_remove_sidebar' );
Run Code Online (Sandbox Code Playgroud)