WordPress:以正确的顺序覆盖子主题中的多个CSS文件 [英] WordPress: override multiple css files in child theme in the proper order

查看:113
本文介绍了WordPress:以正确的顺序覆盖子主题中的多个CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从子主题中按顺序将更多来自父主题的css文件排入队列?

How can I enqueue more css files from the parent theme in the child theme and in which order?

注意:先前的方法是使用@import导入父主题样式表,但这不再是最佳实践,因为它将样式表上载两次.

Note: the previous method was to import the parent theme stylesheet using @import, but this is no longer best practice, because it uploads the stylesheet twice.

使父主题样式表入队的正确方法是添加wp_enqueue_scripts动作,并在子主题的 functions.php 中使用wp_enqueue_style().

The correct method of enqueuing the parent theme stylesheet is to add a wp_enqueue_scripts action and use wp_enqueue_style() in the child theme's functions.php.

因此,这是要使用的正确PHP代码:

So, this is the correct PHP code to use:

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );

function theme_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

但是此操作仅上传 style.css ,并且所有主题都有多个CSS文件,那么我该如何将它们全部入队?以及如何保持正确的文件上传顺序?

But this uploads only style.css and all themes have multiple css files, so how can I enqueue all of them? And how can I keep the proper order of uploading files?

例如,除了 style.css ,如何从路径 theme_parent_directory/css/main.css 入队 main.css . em>?

For example, how can I enqueue the file main.css from the path theme_parent_directory/css/main.css in addition to style.css?

我希望我的英语很清楚. :)

I hope that my English is clear. :)

提前谢谢!

推荐答案

<?php

  add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles',999 );
  function theme_enqueue_styles() {
     wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
     wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/css/main.css' );
     wp_enqueue_style( 'child-style',
      get_stylesheet_directory_uri() . '/style.css',
      array( $parent_style )
     );
}

?>

这篇关于WordPress:以正确的顺序覆盖子主题中的多个CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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