Woocommerce 如何从主题中删除 woocommerce.css 文件? [英] Woocommerce how to remove the woocommerce.css file from the theme?

查看:33
本文介绍了Woocommerce 如何从主题中删除 woocommerce.css 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WordPress 并安装了 Woocommerce 插件.

我在我的主题中添加了模板文件夹并开始自定义我的主题.

现在,我必须从我的主题中删除 Woocommerce.css,我在官方网站上找到了代码

function customtheme_scripts() {wp_enqueue_style('customtheme-style', get_stylesheet_uri(), array(), time());wp_style_add_data('customtheme-style', 'rtl', 'replace');wp_enqueue_script('customtheme-navigation', get_template_directory_uri() .'/js/navigation.js', array(), _S_VERSION, true );if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {wp_enqueue_script('评论回复');}wp_dequeue_style('customtheme-woocommerce-style');}add_action('wp_enqueue_scripts', 'customtheme_scripts');

查看源代码

域名只是一个例子.

解决方案

此答案已在 woocommerce 5.x+ 上进行了全面测试,并且在默认的 woo 样式表上运行良好!如果您使用自定义主题和/或自定义样式表,那么您可能会遇到一些差异.

您在文档页面上看到的内容不再适用于 woo 4+根据他们的github页面.

所以你需要出列它的样式!

<块引用>

wp_dequeue_style文档

因此,如果您只想删除 woocommerce.css 文件,那么您可以这样做:

add_action('wp_enqueue_scripts', 'removing_woo_styles');函数removing_woo_styles(){wp_dequeue_style('woocommerce-general');//这是woocommerce.css";文件}

然而,如果你想删除所有由 woo 加载的样式表,那么你可以使用这个:

add_action('wp_enqueue_scripts', 'removing_woo_styles');函数removing_woo_styles(){wp_dequeue_style('wc-block-vendors-style');wp_dequeue_style('wc-block-style');wp_dequeue_style('woocommerce-general');wp_dequeue_style('woocommerce-layout');wp_dequeue_style('woocommerce-smallscreen');}

如果您仍然可以看到样式,请尝试清理缓存.


与问题自定义样式表"相关的更新

在我撰写答案时,您没有提供任何样式表的屏幕截图,也没有提及使用自定义样式表的任何内容.这就是您无法让它工作的原因.

<块引用>

如果您使用的是自定义样式表(例如问题中使用的自定义 css 文件),请不要复制/粘贴.wp_dequeue_style 函数将您的样式表句柄作为参数.所以请先阅读文档.您使用的是自定义句柄名称(即customtheme-woocommerce-style"),因此,您需要使用该句柄名称.

add_action('wp_enqueue_scripts', 'removing_woo_styles');函数removing_woo_styles(){wp_dequeue_style('customtheme-woocommerce-style');//这是您的自定义样式表";文件.}

另请注意,注释掉主文件(即 inc/woocommerce.php)中的 enqueue 部分可能会暂时起作用,但在下一次 woo 更新时,它会再次出现.因此,建议尽可能避免更新您的模板文件,除非您确实必须这样做,但此处并非如此!

I am using WordPress and I installed the Woocommerce plugin.

I added the template folder in my theme and started to customize my theme.

Now, I have to remove the Woocommerce.css from my theme and I found code on the official website here

I added the same code in the function.php

add_filter( 'woocommerce_enqueue_styles', '__return_empty_array' );

or

add_filter( 'woocommerce_enqueue_styles', '__return_false' );

but both answers are not working. I have installed the 5.7.1 Woocommerce.

Would you help me out to solve this issue?

function.php

function customtheme_scripts() {
    wp_enqueue_style( 'customtheme-style', get_stylesheet_uri(), array(), time() );
    wp_style_add_data( 'customtheme-style', 'rtl', 'replace' );
    wp_enqueue_script( 'customtheme-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _S_VERSION, true );
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
    wp_dequeue_style( 'customtheme-woocommerce-style' );
}
add_action( 'wp_enqueue_scripts', 'customtheme_scripts' );

view source

The domain name is just an example.

解决方案

This answer has been fully tested on woocommerce 5.x+ and works fine on the default woo style sheets! If you're using a custom theme and/or custom style sheet then you may encounter some discrepancies.

What you see on the documentation page, no longer works on woo 4+, according to their github page.

So you need to dequeue its styles!

wp_dequeue_styleDocs

So if you only want to remove woocommerce.css file, then you could do this:

add_action('wp_enqueue_scripts', 'removing_woo_styles');

function removing_woo_styles()
{
  wp_dequeue_style('woocommerce-general'); // This is "woocommerce.css" file
}

However, if you want to remove all of the style sheets loaded by woo, then you could use this:

add_action('wp_enqueue_scripts', 'removing_woo_styles');

function removing_woo_styles()
{
  wp_dequeue_style('wc-block-vendors-style');
  wp_dequeue_style('wc-block-style');
  wp_dequeue_style('woocommerce-general');
  wp_dequeue_style('woocommerce-layout');
  wp_dequeue_style('woocommerce-smallscreen');
}

If you still can see the styles, then try to clean your cache.


UPDATE related to the question "custom style sheet"

At the time that I was writing the answer you had not provided any screenshot of your style sheet, nor did you say anything about using a custom style sheet. That's why you were not able to get it to work.

Please do NOT copy/paste if you're using a custom style sheet, like the custom css file used in the question. wp_dequeue_style function takes your style sheet handle as the argument. So please read the documentation first. You're using a custom handle name (i.e "customtheme-woocommerce-style"), therefore, you need to use that handle name.

add_action('wp_enqueue_scripts', 'removing_woo_styles');

function removing_woo_styles()
{
  wp_dequeue_style('customtheme-woocommerce-style'); // This is your "custom style sheet" file.
}

Also note that commenting out the enqueue section in the main file (i.e inc/woocommerce.php) may work temporarily but on the next woo update, it'll come back again. So, it's recommended to avoid updating your template files as much as possible unless you really have to which is not the case here!

这篇关于Woocommerce 如何从主题中删除 woocommerce.css 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆