子主题css不覆盖父主题

Ser*_*rgi 4 css php wordpress parent-child

我创建了第二十七个主题的子主题。在孩子的主题文件夹中,我有一个“ style.css”和一个“ functions.php”文件。在style.css文件中,我具有:

/* 
Theme Name: Personal_theme
Theme URI: http://wordpress:8888/
Description: This is a child theme of twentyseventeen
Author: My name
Author URI: ...
Template: twentyseventeen
Version: 0.1;
*/

.entry-title {
    color: blue;
}
Run Code Online (Sandbox Code Playgroud)

并在functions.php内部:

<?php
function my_theme_enqueue_styles() {

    $parent_style = 'parent-style'; 

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get("Version")
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>
Run Code Online (Sandbox Code Playgroud)

如果我添加“!重要!” 风格,它会变成蓝色。如果使用检查器工具,则可以看到在父级之后加载了子样式表,但是父级覆盖了样式。我是Wordpresss的新手,functions.php中的任何参数是否错误?我必须改变一些东西吗?

小智 5

这个问题很可能是由CSS选择器特异性引起的。本质上,父css规则比您要分配的规则更狭义地适合打中该元素。您可以使用比父css更具体的规则来接管css,或者使用相同的规则,在这种情况下,您的css将具有优先权,因为稍后会加载。