【问题标题】:dequeue (remove) woocommerce css stylesheets出队(删除)woocommerce css 样式表
【发布时间】:2015-05-16 17:50:27
【问题描述】:

我正在尝试从除结帐/购物车/收据页面之外的所有页面中取出 woocommerce 样式表。

woocommerce 帮助页面 (http://docs.woothemes.com/document/disable-the-default-stylesheet/) 中的代码用于完全删除样式表。

add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

我尝试使用此代码,但它不起作用:

add_action( 'wp_enqueue_scripts', 'child_manage_woocommerce_styles', 99 );
function child_manage_woocommerce_styles() {
remove_action( 'wp_head', array( $GLOBALS['woocommerce'], 'generator' ));
if ( function_exists( 'is_woocommerce' ) ) {
    if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
}

【问题讨论】:

  • 我正在尝试使用 add_filter('woocommerce_enqueue_styles', '__return_empty_array');。但是,它对我不起作用。我将代码放在我的子主题的functions.php 中。任何想法为什么它不起作用?

标签: php wordpress woocommerce


【解决方案1】:

这是因为您正在检查页面不是一次全部。您必须将 if 语句分开,例如:

if ( ! is_woocommerce()) {
  add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
if ( ! is_cart() ) {
  add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}
if ( is_checkout() ) {
  add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
}}

这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    用 || 改变 &&

        if ( function_exists( 'is_woocommerce' ) ) {
        if ( ! is_woocommerce() || ! is_cart() || ! is_checkout() ) {
    add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );
    }}
    

    【讨论】:

      【解决方案3】:

      取消注册/出列样式是最佳实践

      https://codex.wordpress.org/Function_Reference/wp_deregister_style https://codex.wordpress.org/Function_Reference/wp_dequeue_style

      但您也可以使用此过滤器,以过滤掉任何条件下的 woocommerce 样式。

      add_filter( 'style_loader_src', function($href){
      if(strpos($href, "name-of-allowed.css") !== false) {
      return $href;
      }
      return false;
      });
      

      【讨论】:

        猜你喜欢
        • 2016-02-24
        • 2016-09-12
        • 2014-08-25
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 2012-05-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多