使用下面的代码,我从Divi子主题集成了CSS文件。
add_action( 'wp_enqueue_scripts', 'divi_engine_dynamic_child_theme', 20 );
function divi_engine_dynamic_child_theme() {
wp_enqueue_style( 'parent-theme', get_template_directory_uri() . '/style.css', array(), et_get_theme_version() );
wp_dequeue_style( 'divi-style' );
wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
}下面是从主题:缓存-内联样式加载的动态CSS文件。
<link rel='stylesheet' id='et-core-unified-tb-63-tb-46-48-cached-inline-styles-css' href='https://domain.de/wp-content/et-cache/48/et-core-unified-tb-63-tb-46-48.min.css?ver=1630180326' type='text/css' media='all' />CSS文件在Divi>core>components>PageResource.php的第454行中生成。
/**
* Enqueues static file for provided style resource.
*
* @param ET_Core_PageResource $resource
*/
protected static function _enqueue_style( $resource ) {
if ( 'footer' === self::$current_output_location ) {
return;
}
// Bust PHP's stats cache for the resource file to ensure we get the latest timestamp.
clearstatcache( true, $resource->path );
$can_enqueue = 0 === did_action( 'wp_print_scripts' );
// reason: We do this on purpose when a style can't be enqueued.
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet
$template = '<link rel="stylesheet" id="%1$s" href="%2$s" />';
// phpcs:enable
$timestamp = filemtime( $resource->path );
if ( $can_enqueue ) {
wp_enqueue_style( $resource->slug, set_url_scheme( $resource->url ), array(), $timestamp );
} else {
// reason: this whole file needs to be converted.
// phpcs:disable ET.Sniffs.ValidVariableName.UsedPropertyNotSnakeCase
$timestamp = $timestamp ?: ET_CORE_VERSION;
$slug = esc_attr( $resource->slug );
$scheme = esc_url( set_url_scheme( $resource->url . "?ver={$timestamp}" ) );
$tag = sprintf( $template, $slug, $scheme );
$onload = et_core_esc_previously( self::$_onload );
// phpcs:enable
$tag = apply_filters( 'et_core_page_resource_tag', $tag, $slug, $scheme, $onload );
print( et_core_esc_previously( $tag ) );
}
$resource->enqueued = true;
}有人知道我如何将子主题CSS文件作为最后一个css文件加载吗?
发布于 2021-08-31 11:31:00
使用钩子页脚,我可以在以后加载定制的CSS!
add_action( 'wp_enqueue_scripts', 'custom_styles', 105 ); // 102 is the latest used number from parent theme
function custom_styles() {
//wp_enqueue_style( 'parent-theme', get_template_directory_uri() . '/style.css', array(), et_get_theme_version() );
wp_dequeue_style( 'divi-style' );
wp_deregister_style( 'divi-style' );
//wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
}
// add Child Theme CSS as last
add_action('wp_footer', 'custom_styles_footer');
function custom_styles_footer() {
wp_enqueue_style( 'child-theme', get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
}给安德烈的功劳来自优雅的主题与钩子的提示。
发布于 2021-08-30 04:05:02
如果它不在生产中,您可以尝试禁用Divi主题选项> Builder > Advanced中的静态CSS文件生成。
它的接缝更新导致这个停止工作。
这里有一个对这个片段的更新:https://diviengine.com/set-divi-child-theme-dynamic-css/#comment-175924 (不是为我工作)。
https://stackoverflow.com/questions/68967761
复制相似问题