【问题标题】:Style.css not overwriting parent style.css although style.css and functions.php are set up尽管设置了 style.css 和 functions.php,但 Style.css 不会覆盖父 style.css
【发布时间】:2020-05-07 07:19:06
【问题描述】:

这两天我一直在尝试解决这个问题,在这里查看不同的答案。

每当我在 WP 的“附加 CSS”编辑器中编辑 CSS 时,我都会得到想要的结果。但是,我似乎无法从 style.css 表中执行此操作。并不是说我需要从那里开始,但它表明我的子主题设置不正确。

我的 style.css 文件:

/*
Theme Name: Coblog-child
Template: coblog
Author: [name]
Description: Coblog Child
Version: 1.1.0.1588784301
Updated: 2020-05-06 16:58:21
*/

我的函数.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' );
}
?>

知道这两者有什么问题吗?

谢谢

【问题讨论】:

    标签: php css wordpress parent-child


    【解决方案1】:

    您只是在您的functions.php 中加载父样式。 所以你不要加载你的子主题的 style.css。 为了让它工作,你也需要将你的子主题的样式表加入队列:

    function child_theme_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'child-theme-css', get_stylesheet_directory_uri() .'/style.css' , array('parent-style'));
    }
    add_action( 'wp_enqueue_scripts', 'child_theme_styles' );
    

    对于template目录uri(父主题)

    get_template_directory_uri()
    

    对于子主题目录uri

    get_stylesheet_directory_uri()
    

    【讨论】:

    • 太棒了——这似乎成功了。有没有一种安全的方法来确定我的孩子主题实际上是否设置正确?我想避免任何未来的错误。另外,加载父主题 style.css 是否应该出现在我的子主题样式表中?我想直接在工作表中编辑一些元素,并想知道从父级到子级的简单复制粘贴(整个工作表)是否可以。谢谢!
    • 如果主题有问题,您会收到警告或错误。 - 子主题继承父主题的所有样式属性和功能。但是,子主题中的个别调整会覆盖原始主题的设置。所以你不需要复制整个代码部分,子主题具有父主题的风格。但是如果你使用相同的classes或ids,style属性会被覆盖,因为child在parent style之后(代码从上到下执行)。如果您对它感到满意,请将答案标记为已接受,谢谢:)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 2018-08-19
    • 2019-05-27
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    相关资源
    最近更新 更多