WooCommerce-如何移除侧边栏

10
我需要在我的WooCommerce商店中移除侧边栏。 我已经尝试了后台选项主题和每个类别,但都没有结果。
我还尝试了以下方法: 1.编辑文件wp-template-hooks.php并删除do_action(..。 2.编辑文件archive-product.php并删除do_action(..。 3.将remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);插入到主题目录中的function.php和WooCommerce目录中的function.php中。
但以上方法均无效。
我已经想不出其他办法了。 侧边栏的可见性或变量存储在数据库中的哪个表中?
你能帮我吗?
谢谢 Ale
10个回答

12
我只是想为WooCommerce版本3.2.3 - 2017更新这篇文章。
好的,WooCommerce插件包含一个包含所有模板文件的文件夹,如果您想对模板进行更改,可以将所有所需的模板复制到名为woocommerce的文件夹中,该文件夹位于您的根主题文件夹中。这将覆盖插件模板,并允许在覆盖自定义更改的同时进行WooCommerce更新。这些模板在注释中包含所有的do_actions和hooks,因此很容易理解它的工作原理。
话虽如此,WooCommerce有一些钩子,允许您添加或删除代码块,您可以在这里查看API文档
要删除侧边栏,您需要将以下内容添加到您的主题设置函数中的functions.php文件中。
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );

更新: 您还可以通过在页面编辑器中更改页面模板来删除“我的账户”和“购物车”页面上的侧边栏。

enter image description here

enter image description here

选择一个没有侧边栏的模板,或者为主题创建一个新模板,并删除带有<?php get_sidebar(); ?>的行。 希望这能帮到你。

2
无法工作 - 在我的账户页面和购物车上都不行! - AlphaX
我也没成功。 - undefined
听起来像是你的主题模板文件。你可以通过编辑页面并从页面属性侧边栏更改模板为全宽或不包含侧边栏的任何模板,或者创建一个新的模板文件并从模板中删除 <?php get_sidebar(); ?> 来更改我的账户和购物车页面的模板。 - undefined

9
请将以下代码添加到您的functions.php文件中。
function mb_remove_sidebar() {
    return false;
}

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

谢谢!这确实在所有WooCommerce页面上去掉了侧边栏。/** 结束 #4 */ - AlphaX

3
在functions.php中添加以下行:
add_action( 'init', function () {
    remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
} );

我确认它在WooCommerce 6.3.1中可用。 - Café
是的,这在WP 6.3.1和WooCommerce版本8.0.3上都可以运行。 - undefined

1

您已经在主题中集成了WooCommerce吗?

如果没有,请尝试执行将WooCommerce集成到您的主题中这三个步骤。

之后,从your_theme/woocommerce.php中删除get_sidebar();

对我来说它很好用。屏幕截图在此处

希望能帮到您。


是的,Woocommerce已经集成在主题中了。在主题的Woocommerce.php文件中,我尝试删除get_sidebar()函数,但没有任何效果。 - Alessandro Gnola
任何选项都对我无效。 我尝试从我的主题中的woocommerce.php文件中删除操作和删除,但没有起作用... - Guy Ytzhak

1
在functions.php文件中添加以下行:
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);

1

继@maksim-borodov的回答之后,我想有条件地隐藏侧边栏,即仅在产品页面上隐藏。以下是方法:

function remove_wc_sidebar_conditional( $array ) {

  // Hide sidebar on product pages by returning false
  if ( is_product() )
    return false;

  // Otherwise, return the original array parameter to keep the sidebar
  return $array;
}

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

1
一个特殊情况:DIVI主题有侧边栏设置,包括WooCommerce,在主题选项>常规中。

1
更具体地说,主题选项 -> 构建器 -> 产品布局 谢谢 Divi! - blubberbo

0

我正在使用GeneratePress(免费版),所以其他解决方案对我无效。

这个页面给了我答案:https://docs.generatepress.com/article/sidebar-layout/#sidebar-filter

add_filter( 'generate_sidebar_layout', function( $layout ) {
    // If we are on a woocommerce page, set the sidebar
    if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
        return 'no-sidebar';
    }
    // Or else, set the regular layout
    return $layout;
 } );

-3

这可以通过WooCommerce侧边栏插件完成。如果您想通过代码进行删除,请将此代码添加到functions.php中,

  if (!class_exists( 'WooCommerce' ) ) {return;}

  if(is_cart() || is_checkout() || is_product() || is_shop() || is_account_page()){ 
  ?>
  <style type="text/css">
    #secondary {
        display: none;
    }
    #primary {
       width:100%;
    }
   </style>
  <?php
 }
}

这只是隐藏,而不是移除。并非一个干净的解决方案。 - Ralf

-4

这里是如何移除侧边栏的方法:

前往 wp-content/plugins/woocommerce

在文件single-product.php中删除以下代码 -

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

接下来编辑文件archive-product.php并删除以下代码 -

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

(提示*在删除此内容之前,请将此页面上的整个代码复制并粘贴到您的硬盘上的单独文本文件中...这样,如果出现任何问题,您就可以随时将原始代码粘贴回此文件夹中)

5
我认为你不应该更改插件文件!尝试从你的主题中删除该操作,而不是更改插件文件。 - Jason
至少你需要将原始文件复制到你的子主题中并在那里进行修改。但是这仍然不会像你想象的那样高效,因为如果WooCommerce更新原始页面的代码,你可能不得不重新对齐。 - AlphaX

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接