【问题标题】:Removing the WooCommerce sidebar the correct way - TwentySeventeen?以正确的方式删除 WooCommerce 侧边栏 - TwentySeventeen?
【发布时间】:2019-10-09 19:13:55
【问题描述】:

我正在开发一个运行 TwentySeventeen 子主题的项目,虽然网站的其他部分没有侧边栏,但 WooCommerce 似乎有。

例如,商店页面有它 - 我已经尝试了一些东西,但没有警告或根本不起作用:

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

do_action( 'woocommerce_after_main_content' );

这不起作用。

然后我尝试做:

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

...这也没有用。

我找到了this answer 并且它有效,但没有使页面全宽(侧边栏仍有空间)并且使用该方法记录的答案的评论不是一个好主意。

我还找到了this answer,但它涉及添加 CSS,我想避免这种情况,因为它不是最可靠的方法,以防将来类名发生变化等...

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

【问题讨论】:

  • ...这被否决了,为什么!?

标签: php wordpress woocommerce hook-woocommerce wordpress-hook


【解决方案1】:

请将此代码添加到您的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);

【讨论】:

  • 第一个选项效果很好 - 非常感谢。有能力时将奖励赏金。
  • 刚刚注意到这有一个不好的副作用 - 它删除了所有页面中的侧边栏,而不仅仅是 WooCommerce 的;换句话说,有些页面我们希望侧边栏保留,但它也删除了这些。我不确定如何更改它,以便它在不是 WooCommerce 时返回预期值。我知道您可以检查 WC 页面,但是如果不是,您如何确定要返回的内容?返回 true 不正确 - 您必须返回正确的预期状态;显然做return is_active_sidebar($index) 最终会导致无限循环。 :(
  • 好的很抱歉听到我已经编辑了我的答案,请试试这个,希望它对你有用——
  • 欢迎@PonzioPilato 投赞成票,这样它也可以帮助其他人......
【解决方案2】:

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

// 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 检查/过滤器。

也许有一种更清洁的方法可以做到这一点,但这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2015-01-31
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 1970-01-01
    相关资源
    最近更新 更多