$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to avoid loading style.css twice in child-theme?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to avoid loading style.css twice in child-theme?

matteradmin8PV0评论

I have twentyseventeen child-theme, and I found this code:

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles',99);
function child_enqueue_styles() {
    $parent_style = 'parent-style';
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
     wp_enqueue_style( 'seventeen-child',get_stylesheet_directory_uri() . '/style.css', array( $parent_style ));
}
if ( get_stylesheet() !== get_template() ) {
    add_filter( 'pre_update_option_theme_mods_' . get_stylesheet(), function ( $value, $old_value ) {
         update_option( 'theme_mods_' . get_template(), $value );
         return $old_value; // prevent update to child theme mods
    }, 10, 2 );
    add_filter( 'pre_option_theme_mods_' . get_stylesheet(), function ( $default ) {
        return get_option( 'theme_mods_' . get_template(), $default );
    } );
}

..

But there is problem that style.css is loading twice. I tried code from Reference Wordpress, but then my overrides files like footer.php, navigation.php from child-theme are not loading.

Could someone help me with it to avoid loading .css twice and keep full functionality?

I have twentyseventeen child-theme, and I found this code:

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles',99);
function child_enqueue_styles() {
    $parent_style = 'parent-style';
    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
     wp_enqueue_style( 'seventeen-child',get_stylesheet_directory_uri() . '/style.css', array( $parent_style ));
}
if ( get_stylesheet() !== get_template() ) {
    add_filter( 'pre_update_option_theme_mods_' . get_stylesheet(), function ( $value, $old_value ) {
         update_option( 'theme_mods_' . get_template(), $value );
         return $old_value; // prevent update to child theme mods
    }, 10, 2 );
    add_filter( 'pre_option_theme_mods_' . get_stylesheet(), function ( $default ) {
        return get_option( 'theme_mods_' . get_template(), $default );
    } );
}

..

But there is problem that style.css is loading twice. I tried code from Reference Wordpress, but then my overrides files like footer.php, navigation.php from child-theme are not loading.

Could someone help me with it to avoid loading .css twice and keep full functionality?

Share Improve this question edited Feb 25, 2019 at 16:03 kibus90 asked Feb 25, 2019 at 15:52 kibus90kibus90 1512 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 4

You usually don't need to enqueue a child theme's stylesheet. The parent theme does that. This is a bit confusing, so I'll explain.

In most themes, Twenty Seventeen included, the stylesheet is loaded like this:

wp_enqueue_style( 'twentyseventeen-style', get_stylesheet_uri() );

The trick to understanding what's going on here is understanding what get_stylesheet_uri() does. When a regular theme is activated, this function returns the URL to the theme's style.css file. However, when a child theme is activated, the function returns the URL for the child theme's style.css file.

This means that when you create a child theme with a style.css file, that file will automatically be enqueued, but the parent theme's won't. So all you need to do in your child theme is enqueue the parent theme's stylesheet:

add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 9 );
function child_enqueue_styles() {
    wp_enqueue_style( 'parent-style', get_parent_theme_file_uri( 'style.css' ) );
}

Note that I set the priority to 9. This means that the parent theme's stylesheet will get enqueued before the child theme's, which will be enqueued at the default priority of 10.

But there is problem that style.css is loading twice.

This function child_enqueue_styles() enqueues two style.css files, one from parent theme and other from child theme.

// This enqueues style.css from parent theme
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );

// This enqueues style.css from child theme 
wp_enqueue_style( 'seventeen-child',get_stylesheet_directory_uri() . '/style.css', array( $parent_style ));

That's why there are two style.css files loaded. The child theme's style.css should only contain your customization and it should not be a copy of Parent theme's style.css file.

In case you are happy with parent theme's style and just want to override some template files, than you can remove this from your code to load only parent theme's style.

wp_enqueue_style( 'seventeen-child',get_stylesheet_directory_uri() . '/style.css', array( $parent_style ));
Post a comment

comment list (0)

  1. No comments so far