首页 > 解决方案 > Wordpress 子主题不会覆盖父主题 css

问题描述

我正在尝试为 Wordpress 主题二十七创建一个子主题。

我创建了一个名为twentyseventeen-child 的目录,并在其中创建了一个functions.php 和style.css 文件。

我无法弄清楚为什么我对子 style.css 所做的任何更改都没有实现。

这是我的孩子 style.css:

/*
Theme Name: Twenty Seventeen Child
Theme URL: http://tylerdevries.com
Description: Twenty Seventeen Child Theme
Author: Tyler DeVries
Author URL: http://tylerdevries.com
Template: twentyseventeen
Version: 1.0.0
Text Domain: twentyseventeen-child
*/

这是我的孩子 functions.php

<?php
 add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
 function my_theme_enqueue_styles() {
     wp_enqueue_style( 'parent-style', get_template_directory_uri() . 
 '/style.css' );
 }
?>

为了测试我的新子主题,我在我的子 style.css 中包含了以下代码

.site-content-contain {
background-color: #d5ffa0;
position: relative;
}

我还尝试了其他简单的 CSS 自定义 - 完全没有影响。

非常感谢对我做错的任何帮助。

标签: phphtmlcsswordpress

解决方案


// Queue parent style followed by child/customized style
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', PHP_INT_MAX);

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

推荐阅读