https://developer.wordpress.org/themes/advanced-topics/child-themes/
建议将样式表排队的方法是添加wp\\u enqueue\\u scripts操作,并在子主题的函数中使用wp\\u enqueue\\u style()。php。如果没有,请创建函数。php在子主题的目录中。子主题函数的第一行。php将是一个开放的php标记(<;?php),之后您可以根据父主题的操作编写php代码。
如果父主题同时加载两个样式表,则子主题无需执行任何操作。
If the parent theme loads its style using a function starting with get_template, such as get_template_directory() and get_template_directory_uri(), 子主题只需使用依赖项参数中的父级句柄加载子样式。
add_action( \'wp_enqueue_scripts\', \'my_theme_enqueue_styles\' );
function my_theme_enqueue_styles() {
wp_enqueue_style( \'child-style\', get_stylesheet_uri(),
array( \'parenthandle\' ),
wp_get_theme()->get(\'Version\') // this only works if you have Version in the style header
);
}
If the parent theme loads its style using a function starting with get_stylesheet, such as get_stylesheet_directory() and get_stylesheet_directory_uri(), 子主题需要同时加载父样式表和子样式表。确保对父样式使用与父样式相同的句柄名称。
add_action( \'wp_enqueue_scripts\', \'my_theme_enqueue_styles\' );
function my_theme_enqueue_styles() {
$parenthandle = \'parent-style\'; // This is \'twentyfifteen-style\' for the Twenty Fifteen theme.
$theme = wp_get_theme();
wp_enqueue_style( $parenthandle, get_template_directory_uri() . \'/style.css\',
array(), // if the parent theme code has a dependency, copy it to here
$theme->parent()->get(\'Version\')
);
wp_enqueue_style( \'child-style\', get_stylesheet_uri(),
array( $parenthandle ),
$theme->get(\'Version\') // this only works if you have Version in the style header
);
}
请参阅上面我用粗体字引用的文本。现在检查父主题是如何加载其样式的,并按照相应的说明和示例进行操作。我的是后者,因此我将我的编码如下:
add_action( \'wp_enqueue_scripts\', \'twentytwentychild_enqueue_styles\' );
function twentytwentychild_enqueue_styles() {
$parentHandle = "twentytwenty-style";
$childHandle = "twentytwentychild-style";
$theme = wp_get_theme();
wp_enqueue_style($parentHandle, get_template_directory_uri() . \'/style.css\',
array(), // if the parent theme code has a dependency, copy it to here
$theme->parent()->get(\'Version\') // this only works if you have Version in the style header
);
wp_enqueue_style($childHandle, get_stylesheet_uri(),
array($parentHandle),
$theme->get(\'Version\') // this only works if you have Version in the style header
);
}