从任何主题中删除 Woocommerce 侧边栏

Remove Woocommerce sidebar from any theme

我正在使用 WordPress 4.9.4 运行 二十七岁儿童主题主题和 Woocommerce 版本 3.3.4。我正在尝试删除侧边栏......我试过使用这个:

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

但是还没找到合适的

如何删除所有边栏?

最好和最简单的方法适用于所有主题 是这样使用 get_sidebar Wordpress 操作挂钩:

add_action( 'get_sidebar', 'remove_woocommerce_sidebar', 1, 1 );
function remove_woocommerce_sidebar( $name ){
    if ( is_woocommerce() && empty( $name ) )
        exit();
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。 已测试并有效。

You might need to make some CSS changes on some html related containers

此代码适用于任何主题,因为所有主题都使用 get_sidebar() 侧边栏的 Wordpress 函数(甚至是 Woocommerce 侧边栏)并且 get_sidebar 操作挂钩位于在此功能代码中。

WooCommerce 在 class WC_Twenty_Seventeen 中针对此特定主题的代码中为侧边栏广告 /** * 关闭二十十七包装。 */

public static function output_content_wrapper_end() {
        echo '</main>';
        echo '</div>';
        get_sidebar();
        echo '</div>';
    }

我用这段代码替换了那个函数

remove_action( 'woocommerce_after_main_content', array( 'WC_Twenty_Seventeen', 'output_content_wrapper_end' ), 10 );
add_action( 'woocommerce_after_main_content', 'custom_output_content_wrapper_end', 10 );

/** * 关闭二十十七包装。 */

function custom_output_content_wrapper_end() {
        echo '</main>';
        echo '</div>'; 
        echo '</div>';
    }

使用 is_active_sidebar 挂钩 - 这应该适用于 任何 主题,因为它是 WordPress 的核心功能:

function remove_wc_sidebar_always( $array ) {
  return false;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_always', 10, 2 );

您还可以使用条件语句仅隐藏某些页面上的边栏,例如在产品页面上:

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 );