Support » Fixing WordPress » Why is it needed to enqueue parent stylesheet in child theme?

  • The 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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • as stated in the documentation “Setting ‘parent-style’ as a dependency will ensure that the child theme stylesheet loads after it.”

    Moderator stephencottontail

    (@stephencottontail)

    In addition, the child theme documentation attempts to be generic enough to cover as many situations as possible, including themes that load their stylesheets in ways that are no longer considered best practice. For example, if an older theme loads its stylesheet in header.php and you create a child theme that uses its own version of header.php, suddenly the parent theme’s stylesheet will not be loaded because WordPress used the child theme’s header.php instead of the parent theme’s.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Why is it needed to enqueue parent stylesheet in child theme?’ is closed to new replies.