What is child theme?
A child theme in WordPress allows you to change the functionality of a theme without having to customize the original/parent theme’s template files. Since the child theme is stored separately, you don’t need to redo the changes next time you update a theme.
How a child theme works?
Once a child theme is activated, WordPress will look for the template files in that child theme folder first. If the file doesn’t exist in the child theme, it will fallback to the parent theme folder. For example, if there is a “header.php” in the child theme, WordPress will use that “header.php”, else it will use the “header.php” file of it’s parent theme.
Creating a child theme
1) Create folder of your child theme in wp-content/themes folder first. For example, if your parent theme folder name is twentyfifteen, you can name the folder of child theme something like twentyfifteen-child.
2) Create a new style.css file in your child theme folder add the following CSS codes into it:
/* Theme Name: Twenty Fifteen Child Description: Child theme for Twenty Fifteen theme Author: Your Name Template: twentyfifteen */ /* write custom css below the line */ /* write custom css above the line */
3) You can add new css or override parent theme’s css values into child theme’s style.css file
Override Parent theme template files in child theme
To override the template files in the parent theme, open the original template file and save a copy to the child theme folder with same filename and folder structure. Basically, the file structure has to match with the parent theme.
For example, let’s say you want to edit the header.php
- Open the header.php file and save a copy in the child theme.
- Now, edit and save the page.php in the child theme
For another example, let’s say you want to edit a file custom-header.php of a the parent theme and it is stored in the “inc” folder.
- Open the custom-header.php file and save a copy in the “inc” folder of the child theme.
- Edit and save the custom-header.php in the child theme.
Child Theme functions.php file
If you want to override any functions of your parent theme, you can create a functions.php file in the child theme folder. It will load before the parent theme functions.php file.
<?php /* custom functions below this line */ /* custom functions above this line */