php Wordpress 子主题 style.css 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39975520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wordpress child theme style.css not working
提问by Gavin Reynoldson
I have created a file structure in the same format as my parent theme. My parent theme is called Alpine and within Alpine there is a functions.php and style.css file. There do not appear to be any additional style.css files.
我创建了一个与父主题格式相同的文件结构。我的父主题称为 Alpine,在 Alpine 中有一个 functions.php 和 style.css 文件。似乎没有任何额外的 style.css 文件。
I have created a directory called Alpine-child and within that I have created a functions.php and style.css file.
我创建了一个名为 Alpine-child 的目录,并在其中创建了一个 functions.php 和 style.css 文件。
I can't work out why any changes I make to the child style.css are not implemented but they are when I make the same changes in parent style.css
我不明白为什么我对子 style.css 所做的任何更改都没有实现,但是当我在父 style.css 中进行相同的更改时,它们就会发生
This is my child style.css:
这是我的孩子 style.css:
/*
Theme Name: Alpine Child
Theme URI: http://www.creative-ispiration.com/wp/alpine/
Description: My first child theme, based on Alpine
Author: MilkshakeThemes
Author URI: http://themeforest.net/user/milkshakethemes
Template: Alpine
Version: 1.0.0
Tags: one-column, two-columns, right-sidebar, fluid-layout, custom-menu, editor-style, featured-images, post-formats, rtl$
Text Domain: alpine-child
*/
This is my child functions.php file:
这是我的子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' );
}
?>
回答by Jrod
Take a look at your <head>
tag. More importantly look at the order of your stylesheets.
看看你的<head>
标签。更重要的是查看样式表的顺序。
The styles from your child theme are being added first and then all the styles from your parent theme. This will cause the styles from the parent theme to override your child theme styles.
首先添加来自子主题的样式,然后添加来自父主题的所有样式。这将导致来自父主题的样式覆盖您的子主题样式。
You can change the priority of your my_theme_enqueue_styles
function to run after the parent by using the third parameter of add_action. This will enqueue your child theme styles last and allow the CSS to work as expected.
您可以my_theme_enqueue_styles
使用add_action的第三个参数更改函数的优先级以在父级之后运行。这将使您的子主题样式最后排入队列,并允许 CSS 按预期工作。
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 11 );
function my_theme_enqueue_styles() {
wp_enqueue_style( 'child-style', get_stylesheet_uri() );
}
?>
回答by Craig Hicks
This worked for me:
这对我有用:
<?php
function my_theme_enqueue_styles() {
$parent_style = 'twentyseventeen-style';
$child_style = 'twentyseventeen-child-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( $child_style, get_stylesheet_uri() );
}
?>
and none of the other answers did.
其他答案都没有。
回答by San Francesco
Craig Hick's answer worked for me and I also realize why my default code from the Wordpress documentation wasn't working.
Craig Hick 的回答对我有用,我也意识到为什么我的 Wordpress 文档中的默认代码不起作用。
<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
$parent_style = 'parent-style'; // This is 'twentyfifteen-style' for the Twenty Fifteen theme.
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')
); }
In my case, the line wp_get_theme()->get('Version')returned 1.0.0which was the same version number as the parent theme. So I just changed the child theme css version number to 1.0.1and it worked.
就我而言,wp_get_theme()->get('Version') 行返回1.0.0,它与父主题的版本号相同。所以我只是将子主题 css 版本号更改为1.0.1并且它起作用了。
回答by Avag Sargsyan
You need to enqueue the child theme style.css
so your function should be:
您需要将子主题排队,style.css
以便您的功能应该是:
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' );
Take a look at the documentation.
查看文档。
回答by Vikash Kumar
<?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' );
}
?>