简体   繁体   English

Wordpress样式表问题:资源被解释为样式表,但以MIME类型传输

[英]Wordpress Stylesheet Issue: Resource interpreted as Stylesheet but transferred with MIME type

I'm trying to properly setup my functions.php file in Wordpress. 我正在尝试在Wordpress中正确设置我的functions.php文件。 The theme I've been using has been pulling in the stylesheets and JS files in header.php, so I couldn't make a child theme that overwrote these. 我一直在使用的主题已插入header.php中的样式表和JS文件,因此我无法创建覆盖这些主题的子主题。 I'm trying to enqueue the stylesheets and JS files in functions.php instead of header.php. 我正在尝试将stylesheets和JS文件排入functions.php而不是header.php。 However, none of the styling is showing up, and i'm getting this error: 但是,没有样式显示出来,并且我收到此错误:

Resource interpreted as Stylesheet but transferred with MIME type 资源解释为样式表,但以MIME类型传输

Here's the original header.php: 这是原始的header.php:

<!-- WP Linked Resources -->
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" 
type="text/css" media="screen" />
<link rel="shortcut icon" href="<?php echo 
get_stylesheet_directory_uri(); ?>/favicon.ico" />

<!-- Bootstrap -->
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ? 
>/assets/bootstrap.min.css">

<!-- MegaMenu stylesheet -->
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ? 
>/assets/megamenu.css">

<!-- Webfont Kit -->
<link rel="stylesheet" type="text/css" href="<?php 
bloginfo('template_directory'); ?>/assets/MyFontsWebfontsKit.css">

<!-- less -->
<link rel="stylesheet/less" type="text/css" href="<?php 
bloginfo('template_directory'); ?>/assets/stylesheet.less">
<script src="<?php bloginfo('template_directory'); ?>/assets/less.js" 
type="text/javascript"></script>

<!-- print CSS -->
<link rel="stylesheet" href="<?php bloginfo('template_directory'); ? 
>/assets/print.css" type="text/css" media="print" />

<!-- google translate -->
<meta name="google-translate-customization" content="d4ae87e62341359b- 
83f2b047a4c8818c-g4cbb26cb8a5f11dd-11"></meta>

<!-- Android scroll bug fix -->
<script type="text/javascript" src="<?php 
bloginfo('template_directory'); ?>/assets/touchscroll.js"></script>

<?php wp_head(); ?>

Here's functions.php, where I'm trying to enqueue all the files that were in the original header.php 这是functions.php,在这里我尝试使原始header.php中的所有文件入队

function add_theme_scripts() {
wp_enqueue_style( 
    'generic-style',
get_stylesheet_directory_uri() );

wp_enqueue_style( 
  'favicon',
  get_stylesheet_directory_uri().'favicon.ico');

wp_enqueue_style( 
    'bootstrap',
    get_stylesheet_directory_uri(). '/assets/bootstrap.min.css');

wp_enqueue_style( 
    'megamenu-stylesheet',
    get_stylesheet_directory_uri(). '/assets/megamenu.css');  

wp_enqueue_style( 
    'webfont-kit',
    get_stylesheet_directory_uri().'/assets/MyFontsWebfontsKit.css'); 

wp_enqueue_style( 
    'less',
    get_stylesheet_directory_uri().'/assets/stylesheet.less');

wp_enqueue_script( 
    'less-js',
    get_stylesheet_directory_uri().'/assets/less.js');

wp_enqueue_style( 
  'print-css',
  get_stylesheet_directory_uri().'/assets/print.css');

}
add_action( 'wp_enqueue_scripts', '/add_theme_scripts' );

Any suggestions on how to enqueue my css and JS properly? 关于如何正确放入CSS和JS的任何建议?

You should try using the get_template_directory_uri() if the css and js is located in your theme folder. 如果css和js位于主题文件夹中,则应尝试使用get_template_directory_uri()。

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {

wp_enqueue_style( 'generic-style', get_stylesheet_directory_uri() );

wp_enqueue_style( 'favicon', get_template_directory_uri() . '/favicon.ico');

wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/bootstrap.min.css');

wp_enqueue_style( 'megamenu-stylesheet', get_template_directory_uri() . '/assets/megamenu.css');

wp_enqueue_style( 'webfont-kit', get_template_directory_uri() .'/assets/MyFontsWebfontsKit.css'); 

wp_enqueue_style( 'less', get_template_directory_uri() .'/assets/stylesheet.less');

wp_enqueue_script( 'less-js', get_template_directory_uri() .'/assets/less.js');

wp_enqueue_style( 'print-css', get_template_directory_uri() .'/assets/print.css');
}

SOLUTION

So I found that my stylesheet.less was being transferred with MIME type text/plain, because a browser doesn't know how to interpret less. 因此,我发现我的stylesheet.less是使用MIME类型的text / plain传输的,因为浏览器不知道该如何解释。 I compiled my less into css by opening my terminal, going to the wp-content > themes > mytheme > assets (where my stylesheet.less lives) and typing the following: 通过打开终端,进入wp-content>主题> mytheme>资产(我的stylesheet.less所在的位置),然后键入以下内容,将我的代码编译成CSS:

npm install -g less

lessc stylesheet.less > stylesheet.css

I then updated functions.php to enqueue the correct stylesheet: 然后,我更新了functions.php以加入正确的样式表:

wp_enqueue_style( 
'less',
get_stylesheet_directory_uri().'/assets/stylesheet.css');

That should do it! 那应该做!

NOTE I was still having issues with getting my site to render styles properly, so I went through all the files I enqueue and commented them out one by one to see if that fixed the issue. 注意我在使网站正确呈现样式方面仍然存在问题,因此我浏览了我排队的所有文件,并逐一注释掉它们,以查看是否能解决问题。 I found that my print.css file was the culprit, so I commented that out and my site is working properly. 我发现我的print.css文件是罪魁祸首,所以我将其注释掉,并且我的网站正常运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Wordpress资源被解释为样式表,但以mime类型text / html传输 - Wordpress resource interpreted as stylesheet but transferred with mime type text/html 资源解释为样式表,但使用 MIME 类型 text/html 传输:“<url> ”</url> - Resource interpreted as Stylesheet but transferred with MIME type text/html: “<URL>” 资源解释为样式表,但使用MIME类型text / html - apache2进行传输 - Resource interpreted as Stylesheet but transferred with MIME type text/html - apache2 nginx 无法识别 css 和图像:资源被解释为样式表,但使用 MIME 类型 text/html 传输 - css and images not recognized by nginx: Resource interpreted as Stylesheet but transferred with MIME type text/html Symfony CMF CSS未加载:资源被解释为样式表,但以MIME类型text / html传输: - Symfony CMF CSS not loading: Resource interpreted as Stylesheet but transferred with MIME type text/html: 资源被解释为样式表,但是使用MIME类型text / html和在PHP中生成的css进行传输 - Resource interpreted as Stylesheet but transferred with MIME type text/html with css generated in PHP 使用 MIME 类型 text/html 传输的样式表 - Stylesheet transferred with MIME type text/html 资源解释为图像,但使用MIME类型text / html,curl,php进行传输 - Resource interpreted as Image but transferred with MIME type text/html, curl, php 资源被解释为文档,但以MIME类型image / png传输 - Resource interpreted as Document but transferred with MIME type image/png 资源被解释为图像,但由于MIME类型text / html错误而传输 - Resource interpreted as Image but transferred with MIME type text/html error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM