首页 > 解决方案 > 我的 wordpress 子主题不应用 css 更改

问题描述

我从 nightingale 主题创建了一个子主题,但是当我尝试更改 nigthingale-child/style.css 中的属性时,它不适用。例如,我尝试更改正文的背景颜色或为 h1 标记不显示任何内容,以查看我的子主题 style.css 是否有效,但没有任何反应。

我已经按照创建子主题的所有步骤进行操作:-new 文件夹 nightingale-child -在这个 foder 中我添加了两个文件:function.php 和 style.css 这是我的 style.css:

/*
 Theme Name: nightingale-child
 Theme URI:    http://example.com/twenty-fifteen-child/
 Description:  nightingale Child Theme
 Author:       John Doe
 Author URI:   http://example.com
 Template:     nightingale
 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:  nightingalechild
*/

a {
color: red;
}


这是我的function.php:

<?php 
add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

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

有人知道我的夜莺儿童主题有什么问题吗?

标签: wordpress

解决方案


您的函数正在加载 PARENT 主题 CSS。您需要更改一些内容来加载 CHILD CSS:

<?php 
// Let's change this to `enqueue_child_styles` so it's clearer.
add_action( 'wp_enqueue_scripts', 'enqueue_child_styles' );

// Now the function
function enqueue_child_styles() {
    // Rename the hook to child-style
   wp_enqueue_style( 'child-style', get_stylesheet_directory_uri().'/style.css' );
}

在这种情况下,您要使用as 始终指向 PARENT 主题,而 asget_stylesheet_directory_uri()指向CHILD 主题。get_template_directory_uri()get_stylesheet_directory_uri()


推荐阅读