以正确的方式删除 WooCommerce 侧边栏 - TwentySeventeen?

Removing the WooCommerce sidebar the correct way - TwentySeventeen?

我正在做的项目是 运行 TwentySeventeen 的子主题,虽然网站的其余部分没有侧边栏,但 WooCommerce 似乎有侧边栏。

例如,商店页面有它 - 我已经尝试了一些东西并且 none 在没有警告的情况下工作或者根本没有工作:

我尝试将 archive-product.php 复制到 woocommerce/archive-product.php 中的主题目录并删除以下内容:

do_action( 'woocommerce_after_main_content' );

这没有用。

然后我尝试这样做:

remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);

...这也没有用。

我找到 并且它有效,但没有使页面全宽(边栏仍然有 space)并且对使用该方法记录的答案的评论不是好主意。

我还发现了 this answer,但它涉及添加 CSS,这是我想避免的,因为它不是最可靠的方法,以防万一 class 名称发生变化未来等等...

难道没有一种正确的方法可以做到这一点而不会产生潜在的副作用吗?

请将此代码添加到您的 functions.php

仅删除 woocommerce 侧栏
 function disable_woo_commerce_sidebar_mms() {
        remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10); 
    }
    add_action('init', 'disable_woo_commerce_sidebar_mms')
用于删除所有侧栏
function remove_sidebar_mms() {
        return false;
    }

    add_filter( 'is_active_sidebar', 'remove_sidebar_mms', 10, 2 );

你可以试试这个来增加优先级希望充分发挥它的作用

remove_action('woocommerce_sidebar','woocommerce_get_sidebar',25);

在 Mannu saraswat 的 的帮助下,我想出了一个解决方案:

// Remove the sidebar
add_action('get_header', 'blm_wc_remove_sidebar_check', 10);

// Removes the sidebar

function blm_wc_remove_sidebar($index) {
    return false;
}

// Check to see if we're on a WooCommerce page and if so, remove the sidebar

function blm_wc_remove_sidebar_check() {
    if ( is_woocommerce() ) {
        add_filter('is_active_sidebar', 'blm_wc_remove_sidebar', 10, 1);
    }
}

这避免了必须在非 WooCommerce 页面上执行 is_active_sidebar 检查/过滤器添加。

也许有更简洁的方法来执行此操作,但这对我有用。