Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Evan Fraser
Evan Fraser
6,789 Points

Enqueue parent styles always and child styles if child theme is active

I have this line active in my parent functions.php file on my custom theme, just wanting to make sure this is the correct way to activate parent styles always and then additionally a child stylesheet when the child theme is active. It's working but im not sure if this is a conventional method.

Also wanting to see how I would go about enqueueing a child theme javascript file?

<?php

/* --------------------------------------------------------------------------
 * Conditional parent styles
 * -------------------------------------------------------------------------- */
function hawp_conditional_styles() {
    // always load parent styles
    wp_enqueue_style( 'hawptheme', get_template_directory_uri().'/style.css', array(), null );

    // always load child theme stylesheet when active
    if ( is_child_theme() ) {
        wp_enqueue_style( 'hawptheme-child', get_stylesheet_uri(), array(), null );
    }   
}

?>

1 Answer

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,350 Points

Hi Evan,

This seems to be an unusual way to do this. To setup a child theme you only need to create style.css and functions.php files within a child theme folder to control all your child theme logic. The child theme inherits all parent theme settings by default and then any child theme settings will override the parent settings. You should never need to edit your parent theme files.

I don't see any reason to do this differently, unless you have some special requirements. See documentation link on how to do this. https://codex.wordpress.org/Child_Themes