从产品类别页面移除侧边栏| Wordpress Woocommerce



我的问题是,如何仅为特定的产品类别"Slug"而不为其子Slug删除侧边栏。
如果url像下面这样-删除边栏,并使页面全宽仅为"卫浴"one_answers"壁橱"http://www.example.com/product-category/sanitaryware/
http://www.example.com/product-category/sanitaryware/closets/

我不想因为所有"产品类别"的原因而删除侧边栏,我想让侧边栏显示孙子标签"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循环,获取一个词的term对象并检查这个词的父词。在我们的例子中,我们不返回顶层父结点,我们只保留一个count并返回它。

/**
* 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过滤器中删除有问题的操作。

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

一种方法是制作自定义模板,并防止在查看产品类别页面时呈现侧边栏。按照Woocommerce文档中的升级安全建议,你应该:

1)进入wp-content/plugins/woocommerce/templates/目录,复制 archive-product.php 文件

2)在你的主题目录中创建一个woocommerce目录(例如wp-content/themes/your-theme/woocommerce)

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

4)查看新自定义文件的底部,找到:

do_action( 'woocommerce_sidebar' );
5)将这行代码替换为:
if (!is_product_category('sanitaryware') && !is_product_category('Faucet')) {
   do_action( 'woocommerce_sidebar' );
}

6)为了使产品类别页面内容是全宽的,你可以使用过滤器在这些页面的<body>中添加一个名为product-category的类。

要添加这个新类,请在functions.php文件中插入以下代码:
// 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选择器来定位产品类别页面:
body.product-category { /* place Product Category page specific CSS here */ }

因为我不知道你的主题的细节,所以我不能告诉你你想要设置100%宽度的HTML元素,但如果页面内容是<div id="content">之类的东西,你可以这样设置CSS:

body.product-category #content { 
    width: 100%;
    float: none;
} 

你可以将侧边栏从类别页面覆盖中移除archive-product.php或者在你当前的WordPress主题中使用function.php中的action hook。

你可以将这些文件复制到你的主题中,而不是直接在插件中编辑这些文件(这是一个非常糟糕的主意,因为一旦更新插件,你所有的更改都会丢失!)

  • 在主题目录下新建一个名为"WooCommerce"的文件夹。导航到WooCommerce插件目录,打开"templates"文件夹。模板文件夹有很多子文件夹,其中包含WooCommerce使用的所有不同模板。幸运的是,WooCommerce中的模板文件结构和命名很容易遵循。
  • 在新创建的"WooCommerce"文件夹中,复制要编辑的模板文件。

请记住此处的目录结构保持不变。如果要编辑的模板位于子文件夹中,请记住在你的主题目录下创建子文件夹。

In archive-product.php -

<?php
/**
 * woocommerce_sidebar hook
 *
 * @hooked woocommerce_get_sidebar - 10
 */
do_action('woocommerce_sidebar');?>

此代码在类别页面上显示侧边栏。删除此代码并保存文件。

要从分类页面中删除侧边栏,您需要在
中使用动作钩子 file -

    add_filter( 'body_class', 'remove_sidebar' );
    function remove_sidebar()
    {
        if( is_product_category()) { 
         remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
       }
    }
接下来,我们要编辑style.css在你的主题文件夹
body.product-category #content
    {
            width: 100%;
    }

在这里你可以得到WooCommerce Action和Filter Hookhttps://docs.woothemes.com/wc-apidocs/hook-docs.html

最新更新