首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从产品类别页中删除边栏

从产品类别页中删除边栏
EN

Stack Overflow用户
提问于 2015-08-23 03:50:34
回答 2查看 8.4K关注 0票数 0

我的问题是,我如何能够删除边栏只针对特定的产品类别“鼻涕虫”,而不是它的子弹头。

如果url如下所示-移除侧栏,使页面完全宽度只适用于鼻涕虫“卫浴器”和“壁橱”http://www.example.com/product-category/sanitaryware/

http://www.example.com/product-category/sanitaryware/closets/

由于“产品类别”的原因,我不想删除侧栏,我希望侧栏显示“一体式壁橱”:

http://www.example.com/product-category/sanitaryware/closets/one-piece-closets

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

代码语言:javascript
复制
  <?php
            /**
             * woocommerce_sidebar hook
             *
             * @hooked woocommerce_get_sidebar - 10
             */
            if (!is_product_category('sanitaryware')){
        do_action('storefront_sidebar');
}
?>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-24 16:30:23

基于您隐藏顶级类别的侧边栏及其直接子类的愿望,我们将需要一个系统来确定任何术语存档的层次“深度”。类似于人们通常获得“顶级”父术语的方式,我们可以执行一个while循环,获取一个术语的术语对象,并检查该术语的父级。在我们的例子中,我们不返回顶级的父级,我们只需要计数并返回它。

代码语言:javascript
复制
/**
* 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侧边栏了:

代码语言:javascript
复制
/**
* 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类来使边栏页面更易于样式,那么我相信您可以在实际过滤body类的同时从body_class过滤器中删除相关操作。

代码语言:javascript
复制
/**
* 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' );
票数 6
EN

Stack Overflow用户

发布于 2015-08-23 06:57:48

一种方法是创建自定义模板,如果正在查看产品类别页面,则防止侧边栏呈现。按照Woocommerce博士提供的升级安全建议,您应该:

1)转到wp-content/plugins/woocommerce/templates/目录并复制archive-product.php文件

2)在主题目录中创建一个woocommerce目录(例如woocommerce// your /woocommerce)

3)创建一个wp-content/themes/your-theme/woocommerce/副本,并将其放在archive-product.php中,并命名为archive-product.php文件

4)在新的自定义文件底部附近查找并找到:

代码语言:javascript
复制
do_action( 'woocommerce_sidebar' );

5)将这一行代码改为:

代码语言:javascript
复制
if (!is_product_category('sanitaryware') && !is_product_category('Faucet')) {
   do_action( 'woocommerce_sidebar' );
}

6)要使产品类别页面内容完全宽度,可以使用筛选器将名为product-category的类添加到这些页面的<body>中。

要添加这个新类,请在functions.php文件中插入以下代码:

代码语言:javascript
复制
// Add specific CSS class by filter
add_filter( 'body_class', 'product_categories' );
function product_categories( $classes ) {
    // add 'class-name' to the $classes array
    if (is_product_category('sanitaryware') && is_product_category('Faucet')) {
        $classes[] = 'product-category';
    }
    // return the $classes array
    return $classes;
}

7)然后,在主题的样式表中,可以使用这个CSS选择器将产品类别页面作为目标:

代码语言:javascript
复制
body.product-category { /* place Product Category page specific CSS here */ }

由于我不知道主题的具体内容,所以我无法告诉您要将目标设置为100%宽度的HTML元素,但是如果页面内容位于类似于<div id="content">的内容中,您可以这样设置CSS:

代码语言:javascript
复制
body.product-category #content { 
    width: 100%;
    float: none;
} 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32162972

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档