【问题标题】:Remove Woocommerce sidebar from any theme从任何主题中删除 Woocommerce 侧边栏
【发布时间】:2018-03-27 20:06:30
【问题描述】:
我正在使用 WordPress 4.9.4 运行 27 个儿童主题主题和 Woocommerce 版本 3.3.4。我正在尝试删除侧边栏...我尝试过使用它:
remove_action('woocommerce_sidebar','woocommerce_get_sidebar',10);
但还没有找到合适的。
如何删除所有侧边栏?
【问题讨论】:
标签:
php
wordpress
woocommerce
sidebar
wordpress-hook
【解决方案1】:
适用于所有主题的最佳且简单的方法是这样使用 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 文件中。经过测试并且可以工作。
您可能需要对一些与 html 相关的容器进行一些 CSS 更改
此代码适用于任何主题,因为所有主题都使用 get_sidebar() Wordpress 功能作为侧边栏(甚至适用于 Woocommerce 侧边栏),get_sidebar 操作挂钩位于此功能代码内。
【解决方案2】:
WooCommerce 在 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>';
}
【解决方案3】:
使用 is_active_sidebar 钩子 - 这应该适用于 any 主题,因为它是 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 );