我已经正确地设置了一个子主题,并在我的新functions.php文件中添加了样式表和脚本。唯一的问题是,当文件位于子主题文件夹中时,将使用父主题引用我新加入的样式表。
设置儿童主题:
<?php
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);
function enqueue_child_theme_styles() {
wp_enqueue_style( 'parent-theme', get_template_directory_uri().'/style.css' );
wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array('parent-theme') );
}
?>将新样式进一步添加到子functions.php文件中:
<?php
function new_scripts() {
wp_enqueue_style( 'grid', get_template_directory_uri() . '/css/grid.css');
}
add_action( 'wp_enqueue_scripts', 'new_scripts' );它以这种方式显示在“呈现时”中:
<link media="all" type="text/css" href="/wp-content/themes/parent-theme/css/grid.css?ver=4.2.2" id="grid-css" rel="stylesheet">我需要它来参考href="/wp-content/themes/child-theme/css/grid.css?ver=4.2.2“
有什么建议吗?
发布于 2015-05-13 05:10:18
您可以在对脚本和样式进行排队时使用get_stylesheet_directory_uri();。它返回存储子样式表的目录路径。
发布于 2019-12-10 14:46:48
只需简单说明一下,如果在您的PHP_INT_MAX调用中包含add_action变量,子主题将无法正常工作!
别这么做..。
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX);你应该这么做..。
add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles');然后它将正确导入子主题。
https://wordpress.stackexchange.com/questions/188187
复制相似问题