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

标签 php css wordpress woocommerce

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

出于所有“产品类别”的原因,我不想删除侧边栏,我希望侧边栏显示孙子 slug“one-piece-closets”:
http://www.example.com/product-category/sanitaryware/closets/one-piece-closets

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

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

最佳答案

根据您希望隐藏顶级类别及其直接子类别的侧边栏的愿望,我们将需要一个系统来确定任何术语文件的层次“深度”。类似于人们通常如何获得“顶级”父项,我们可以执行 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;
}

现在我们有了可以用作条件的东西,我们可以有条件地删除 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' );

编辑/更新

如果您还想添加自定义主体类以使无侧边栏的页面更易于设计样式,那么我相信您可以同时从 body_class 过滤器中删除相关操作实际上过滤了 body 类。

/**
* 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' );

关于php - 从产品类别页面中删除边栏 | WordPress Woocommerce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32162972/

相关文章:

html - CSS - 如何处理位置 :absolute elements when resizing with media queries

php - WP MVC : How to edit and save records involving multiple tables

php - Woocommerce:从最新订单获取数据

php - 如何将查询的多个结果插入另一个表?

php - 帮助构建最佳查询

javascript - Angularjs $http post请求数据和未定义错误的响应

javascript - 使用 javascript 删除 &lt;title&gt; 标签

php - 在 PHP 中删除换行符并添加 BR 标记

html - 将图形与标题对齐

javascript - CSS 中的滑动门动画,如 Twitter 通知