1

WordPress documentation says to enqueue parent stylesheet in child theme:

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {

    $parent_style = 'parent-style';

    // Why enqueue parent style again? The child theme works perfectly without this line
    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')
    );
}

But parent theme stylesheet is already enqueued in parent theme, what's the point to enqueue it again in child theme?

5
  • This maybe answer for you. wordpress.stackexchange.com/questions/218610/…
    – Por
    Jun 23, 2018 at 8:28
  • No, it doesn't answer my question. You can set 'parent-style' as a dependency of child theme stylesheet without having to enqueue it again.
    – Edward
    Jun 23, 2018 at 8:47
  • you have to deregister the parent theme like that : wordpress.stackexchange.com/questions/218610/…
    – mmm
    Jun 23, 2018 at 8:58
  • @mmm, no need to do that, you can try.
    – Edward
    Jun 23, 2018 at 9:17
  • some themes need that in order to set the dependency between parent et child theme.
    – mmm
    Jun 23, 2018 at 10:06

2 Answers 2

0

Not all themes enqueue the stylesheet like that. It was pretty normal a few years ago for a theme to just have the stylesheet link hard coded in header.php

So a child theme couldn’t guarantee that a parent theme had enqueued the stylesheet.

0

But parent theme stylesheet is already enqueued in parent theme

No it's not. Not usually. The reason is that most themes enqueue the stylesheet provided by the get_stylesheet_uri() function. This returns the URL to the style.css file of the current theme.

When you create and activate a child theme the child theme becomes the current theme. So this means that the parent theme is enqueueing the style.css file from the child theme but not enqueueing its own stylesheet.

So if you want to enqueue the parent theme's original stylesheet you need to enqueue its style.css file from the child theme.

Why this is, I can't say, but if I had to guess I'd say that when the feature was introduced child themes were probably mostly intended for replacing the theme's stylesheet. If that were the case then it makes sense that the child theme process wouldn't include loading the existing stylesheet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.