Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
but what I understand is that the child theme’s style.css OVERRIDES the parent’s style.css
It doesn’t necessarily, it really just appends onto it. If your Child Themes styles override the parent theme’s, then that is due to CSS specificity and not an underlying principle that this overriding occurs.
It doesn’t necessarily, it really just appends onto it. If your Child Themes styles override the parent theme’s, then that is due to CSS specificity and not an underlying principle that this overriding occurs.
Okay, so if I wanted to take an existing theme that has been customized, and make a child theme from it — I would cut out just the customized code from style.css and function.php and put them in the child?
Andrew Nevins
(@anevins)
WCLDN 2018 Contributor | Volunteer support
Great, let me give that a shot. Thanks.
Okay, I’ve been experimenting with this for the past 2 hours and here’s what I’ve discovered. Everything with style.css is working the way it should. But here’s the odd thing with the functions.php file.
I installed a brand new theme and created a child theme for it. I then cut out an “if function exists” section from the parent put it into a new functions.php file in the child theme. So, at this point, the only thing the child’s functions.php file has is this particular “if function exists” code.
After this, everything looked normal when viewing the child theme, as expected. The code from the child was evidently appended to the parent.
But then, I modified that code and put it back in the parent. So now the child has the original code and the parent has the modified code. They’re both within the same “if function exists” tags.
When I did this, the child theme’s code completely overwrote the parent, instead of appending it. I exchanged the code between parent and child, and saw the same behavior.
Am I missing something?
Yes.
Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)
http://codex.wordpress.org/Child_Themes#Using_functions.php
Unlike style.css, the functions.php of a child theme does not override its counterpart from the parent. Instead, it is loaded in addition to the parent’s functions.php. (Specifically, it is loaded right before the parent’s file.)
Right, I understand that, but what I’m saying is that my child theme’s functions.php file IS overriding its parent counterpart.
Okay, I think here’s what’s happening. My knowledge of php is basically what I’ve learned in the past few hours, so please excuse the lack of correct terminology.
Anyway, when the parent and child have different code within the same conditional tags, the child’s code will be used, having the same effect as overriding the parent. I’m assuming this is because php code can’t be appended from child to parent within the same tags. However, anything new outside of those tags will be appended.
I don’t know if that’s how it works, but it makes sense if it does. I think.