关于php:Child主题CSS不会覆盖父级

Child theme css not overriding parent

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

1
2
3
4
5
6
7
8
9
10
11
12
13
/*
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;
}

并在functions.php内部:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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' );
?>

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


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


尝试将功能更新为

1
2
3
4
5
6
7
8
<?php
    function my_theme_enqueue_styles() {
      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') );
    }

    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
?>

然后,请提供控制台中实际加载的屏幕截图。

不知道为什么要创建父样式的变量,但是您当然可以继续使用它。

希望这可以帮助


尝试添加

1
 @import url("..twentyseventeen/style.css");

子样式表顶部的.entry标题上方。 这是必需的,以及您的模板名称。 希望这可以帮助。