【问题标题】:WordPress Child Theme not loading style.cssWordPress子主题未加载style.css
【发布时间】:2020-09-13 20:32:48
【问题描述】:

我正在尝试创建一个子主题,functions.php 似乎可以正常工作,但由于某种原因,子主题没有加载。我在我的functions.php中添加了以下内容,它确实有效,只是它加载了父主题的style.css而不是子主题。

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

这是我在孩子中的 style.css 的顶部:

/*
Theme Name: Chaplin Child
Theme URI: https://my-site.com/
description: Child Theme
Template: Chaplin
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: light, dark, two-columns, right-sidebar, responsive-layout, accessibility-ready
Text Domain: stack-child
*/

【问题讨论】:

  • 您的代码告诉 WordPress 仅加载父主题的样式表,仅此而已。这就是为什么它不从您的子主题加载样式表的原因。 documentation 有关于如何加载两个样式表的示例,你试过了吗?
  • 是的,我做到了。 functions.php 代码的第一个版本没有改变任何东西。第二次添加了表单,但是父样式表中的任何样式都会覆盖子样式表中的 CSS。
  • 那么问题是您如何为您的子主题编写 CSS 规则。这是一本好书:CSS Specificity.

标签: css wordpress wordpress-theming


【解决方案1】:

试试这个(functions.php),这是来自我的一个工作子主题,并且还加入了子主题样式表:

add_action('wp_enqueue_scripts', 'register_my_scripts');

function register_my_scripts() {
    wp_enqueue_style( 'parent_style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css'  );
}

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2017-02-19
    • 1970-01-01
    • 2021-01-16
    • 2018-03-24
    • 2015-05-16
    • 2019-05-27
    • 1970-01-01
    • 2021-04-18
    相关资源
    最近更新 更多