0

I am creating a child theme of a theme in WordPress. In Parent theme i have style.css and inside css folder of parent theme i have a base.css file ( and some more css files but the problem is only with base.css file ). Somehow it turns out that the sequence in which styles should be loaded is parent->base.css then parent->style.css the child->style.css. I want to ask whether the code below is correct way to do so ? To be specific it's Enough Theme.

functions.php inside child theme

<?php
    function my_theme_enqueue_styles() {

        $parent_style = 'parent-style';
        wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css', array('enough_base') );

    }
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 9 );
?>
<?php
    function enqueue_child_theme_styles() {
      wp_enqueue_style( 'child-style', get_stylesheet_uri(), array($parent_style) );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', 11);
?>
<?php
2
  • You can use @import in style of your child-theme for inheriting style. Here, is the link to get more details on child theme creation. codex.wordpress.org/Child_Themes
    – BlueSuiter
    Commented Apr 21, 2017 at 19:40
  • @Amit Chauhan yes but in codex it is mentioned that enqueuing way is more efficient and correct way to load style sheets from parent theme
    – Desperado
    Commented Apr 21, 2017 at 19:43

1 Answer 1

1

You can try below code. I think there is no need to use wp_enqueue_scripts twice to enqueue the parent and child theme style sheets separately. Also the use of echo get_stylesheet_uri( ); in the enqueue_child_theme_styles function doesn't make any sense.

function my_theme_enqueue_styles() {

    $parent_style = 'styles';

    wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css', array('enough_base') );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array( $parent_style ),
        wp_get_theme()->get('Version')
    );
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

Make sure that you replace the parent-style with the same $handle used in the parent theme when it registers its stylesheet. You can refer following link for more details. https://codex.wordpress.org/Child_Themes

5
  • The echo statement was for debugging purpose ... edited. Now the problem is that i want the specific order that i mentioned in the question ... base ->parent_style -> child_style. When i try to do it any other way that sequence comes out to be different.When i am using the code you have put in answer child's stylesheet loads before parent's. So changes i make in child's stylesheet won;t reflect.
    – Desperado
    Commented Apr 22, 2017 at 8:02
  • @Desperado - I have tried that above code in my local environment and it is working as expected. Can you share link of your website? If possible. Also, I hope that you have replaced parent-style with handle of your parent theme. Commented Apr 22, 2017 at 9:49
  • I am using enough theme in my local enviornment . Could you try if possible !!!
    – Desperado
    Commented Apr 22, 2017 at 10:16
  • @Desperado - I have edited the code and also added my comments regarding edits. Please check. Commented Apr 22, 2017 at 11:52
  • The problem is still the same ... child theme's style css loads earlier than parent's style css. But with the code that i have put in question things happen correctly. But i don't know whether that is the correct way or not.
    – Desperado
    Commented Apr 22, 2017 at 16:14

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.