【问题标题】:WordPress: override multiple css files in child theme in the proper orderWordPress:以正确的顺序覆盖子主题中的多个 css 文件
【发布时间】:2015-07-11 03:18:51
【问题描述】:

如何将更多的 css 文件从父主题中排入子主题中以及按什么顺序排列?

注意:以前的方法是使用@import 导入父主题样式表,但这不再是最佳做法,因为它会上传两次样式表。

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

所以,这是要使用的正确 PHP 代码:

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 文件,那么我怎样才能将它们全部排入队列?以及如何保持上传文件的正确顺序?

例如,除了 style.css 之外,我如何才能将文件 main.css 从路径 theme_parent_directory/css/main.css 入队em>?

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

提前谢谢你!

【问题讨论】:

  • 解决方案很简单,就是在你的 style.css 文件中使用更具体的规则。

标签: php jquery css wordpress


【解决方案1】:
<?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 )
     );
}

?>

【讨论】:

  • 所以,一般来说,必须从父 functions.php 复制wp_enqueue_style 调用?为什么会这样,因为父母的 functions.php 是在孩子的 functions.php 之后运行的?
猜你喜欢
  • 2015-01-02
  • 2018-12-09
  • 2020-07-28
  • 2019-02-28
  • 1970-01-01
  • 2018-04-07
  • 1970-01-01
  • 2015-08-09
  • 1970-01-01
相关资源
最近更新 更多