• When I create a child theme, the functions.php of the parent (template) theme should always be run, correct? So if there is a functions.php in the child theme, they would both be ran?

    The reason I am asking is my parent them uses wp_enqueue_style to load style sheets. Doing the typical create a style.css for the child them and import the style.css from the parent does not seem to be working 100%. My guess is that some of the style information is being loaded in the parents functions.php using wp_enqueue_style and for some reason that code is not executing as I expect it to?

    The theme I am using is here: https://chimpgroup.com/wp-demo/statfort/

    Any help of suggestions would be greatly appreciated. The theme is supposed to be “child theme compatible”, but they don’t provide a template like others do.

Viewing 15 replies - 1 through 15 (of 16 total)
  • Thread Starter Josh Eby

    (@josheby)

    Anyone have any ideas?? wp_enqueue_style is new to me so maybe that is the issue. Last theme I made a child for was twentyten I think. Thanks!

    Is your child theme active right now (10:47 AM, Mountain time)? The only stylesheet I see being loaded is the parent theme’s stylesheet.

    Thread Starter Josh Eby

    (@josheby)

    I will make a change and post a link to the site with the child theme this evening.

    I am right in my thinking that the functions.php should get loaded for both the child and the parent theme though correct? So even if they are being loaded using wp_enqueue_style and not just part of the style.css, they should all still be there?

    Thank you for being willing to take a look. Greatly appreciated!

    Your understanding is correct. Do you load stylesheets with wp_enqueue_style in your child theme’s functions.php?

    The functions.php in the child theme is run before the functions.php in the parent theme. So hence, both will be loaded before the site shows in that order.

    Since you are using the style.css file in the child theme folder, make sure that the header info in that CSS file is correct in which it refers to the parent theme.

    Thread Starter Josh Eby

    (@josheby)

    What I had done was just create the style.css header and import the style.css from the parent. That was all I remember having to do with twentyten.

    I intend to use wp_enqueue_style as I build upon my child themes after learning what it is. Had never encountered it until recently.

    You don’t need to use wp_enqueue_style if you are using a style.css for the child theme since it will be automatically recognized by WordPress. The only thing is that the header in the file needs to be correct in which the template name refers to the name of the parent theme.

    Also, make sure the child theme is activated!

    Thread Starter Josh Eby

    (@josheby)

    Correct, I am just planning to if I need to include any subsequent style sheets or if I make any widgets etc.

    Well I rebuilt my test site last night and not the styles are working, but I have an issue with the menu…

    The main menu doesn’t appear to work right if I am using the child theme. Link below. I have compared the scripts that are being loaded in the base of using the parent or using the child and don’t see any difference.

    https://test.trhsart.com/

    I have reached out to the theme vendor to see if they have any idea. Unfortunately, while I like their theme, I have not been impressed with their support thus far.

    So any assistance any of you can offer is greatly appreciated. Thanks!!

    Thread Starter Josh Eby

    (@josheby)

    After a bit more looking, it appears the issue only may be if you are logged in. If I log out and then refresh the page the menus are correct. This is not the case however with just the parent theme active.

    I will wait and see what I get from support.

    Thread Starter Josh Eby

    (@josheby)

    Well support logged in and fixed it, but didn’t tell me what they did. So I asked… I am thinking it was an issue with the menu setup in the admin side of things.

    Anyways… thank you to everyone for their time and help!

    Hi respectyoda,

    If I understood what you wrote correctly, if my child style.css has the correct parent theme name and template name in the header, there is no need to put the @import or create the functions.php like mentioned here https://codex.www.ads-software.com/Child_Themes am I right?

    Also, am I correct to assume that the only file from the child theme that does not override its parent version is the functions.php since it loads before the parent one?

    Thanks in advance!

    Hi WPNewbie,

    If you create a child theme, then it needs to have the import declaration to load the parent theme’s stylesheet, but it is better to enqueue it by using the following code in the child’s functions.php file:

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
    
    }

    Yes, the functions.php file of the child theme is loaded right before the parent’s and both are used.

    Thanks a lot for your fast clarification!

    So if for example I used theme “XYZ”, my child theme can contain only the functions.php with the following code right?

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'XYZ', get_template_directory_uri() . '/style.css' );
    
    }

    I read that if the parent theme has many css files it is necessary to enqueue all the css files and the theme I am testing has css files in different subfolders. Would the code in the child functions.php be like this then?

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'XYZ', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'XYZ', get_template_directory_uri() . '/cssfile1.css' );
        wp_enqueue_style( 'XYZ', get_template_directory_uri() . '/cssfile2.css' );
    }

    I have been trying to understand how to use it correctly but the more I read, the more confused I get and on youtube all examples use the @import.

    Thanks a lot in advance! ??

    No, you only need to enqueue the parent’s default stylesheet when dealing with the parent theme. The parent theme’s functions.php file should already enqueue whatever other stylesheets and scripts it has.

    Now, if you are going to include css files IN your child theme folder, then you do need to enqueue them using your child theme’s functions.php file.

    Some older resources use the @import declaration, but this declaration runs slower than the enqueue function, especially when you have many stylesheets and scripts.

    So if I want to make style changes in the theme via a child style.css I would use

    add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
    function theme_enqueue_styles() {
        wp_enqueue_style( 'XYZ', get_template_directory_uri() . '/style.css' );
        wp_enqueue_style( 'XYZ-child', get_template_directory_uri() . '/childstyle.css' );

    Is this correct or is there no need to enqueue the childstyle as it should load after the functions.php?

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Child Theme / wp_enqueue_style’ is closed to new replies.