2

I accidentally discovered that my child theme CSS is get called twice. I found that this is an old issue (see here and here), but I don't know how to solve it in my situation.

<link rel='stylesheet' id='parent-style-css'  href='http://infopsi.md/wp-content/themes/twentyseventeen/style.css?ver=4.8.1' type='text/css' media='all' />
<link rel='stylesheet' id='child-style-css'  href='http://infopsi.md/wp-content/themes/twentyseventeen-child/style.css?ver=0.1' type='text/css' media='all' />
<link rel='stylesheet' id='twentyseventeen-style-css'  href='http://infopsi.md/wp-content/themes/twentyseventeen-child/style.css?ver=4.8.1' type='text/css' media='all' />

This is the function that I use to enqueue the parent and child theme stylesheets:

/** Enqueue the parent and child theme stylesheets **/
if ( !function_exists( 'my_theme_enqueue_styles' ) ):
    function my_theme_enqueue_styles() {
        $parent_style = 'parent-style';
        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')
        );
    }
endif;
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

I tried to add the wp_dequeue_style( 'twentyseventeen-style' ); to the function, but this doesn't solved the issue. Any suggestions?

2
  • 2
    the child style.css can be called by the parent theme (in parent functions.php) then just remove the call in the child functions.php
    – mmm
    Sep 13, 2017 at 7:31
  • @mmm So simple? The part wp_enqueue_style( 'child-style', ... is not needed? I removed that part from my function and it works! Thank you!
    – Iurie
    Sep 13, 2017 at 7:39

1 Answer 1

3

You don't need to enqueue your child theme's stylesheet. The parent theme does that for you. TwentySeventeen has this line:

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

That's still going to run, but twentyseventeen-style is now your child theme's stylesheet. You just need to enqueue the parent theme's stylesheet:

/** Enqueue the parent and child theme stylesheets **/
if ( !function_exists( 'my_theme_enqueue_styles' ) ):
    function my_theme_enqueue_styles() {
        $parent_style = 'parent-style';
        wp_enqueue_style( $parent_style, get_parent_theme_file_uri( 'style.css' ) );
    }
endif;
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

(I swapped your use of the template directory function for get_parent_theme_file_uri() because more people should know about the new theme file functions).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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