How and why to create a child template on a WordPress site

WordPress and the huge number of templates with different features and focus is becoming an increasingly popular and likely choice for a new project. But not every template fits.

What exactly is the child template?

Very briefly, a child template is a copy of your main template that provides the layout of the site. It is recommended for all users of the WordPress content management system who need to make customizations to their WordPress site.

The main advantage of this procedure is that if you make changes to configuration files, e.g. function.php, changes made are not deleted after the template is updated.

 

How do I create a child template?

Step 1: Template folder

In the first step, first create an FTP folder in the wp-content/themes directory. Name the folder according to your current template with the postfix “-child”.

So if your current template is e.g. Divi template, name the folder divi-child.

Step 2: Create a style.css file

In order for WordPress to know that it is a template, you need to create a style.css file that will contain information about the child template authors, your website address, contact information, etc. in the comments.

For example, the recording might look like this:

  1. /*
  2. Theme Name: Divi child šablona
  3. Theme Url: vasedomena.cz
  4. Description: child šablona webu vasedomena.cz
  5. Theme Author: Jan Novák
  6. Author Url: vasedomena.cz
  7. Template: Divi
  8. Verzion: 1.0.0
  9. Text Domain: Divi
  10. */

You can complete the entries after the colons according to your needs.

Step 3: Create a function.php file

In order for the child template to work properly, WordPress needs to be told what styles and scripts to use when displaying the content.

  1. <!--?php add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' ); function enqueue_parent_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); } ?-->

This code needs to be inserted into the newly created function.php file. In the child directory of the template there will now be two files, style.css and function.php.

Step 4: Edit child templates

All modifications, whether related to features or appearance, should now be made in these two files. As mentioned in the introduction, once the master template is updated, your edits will not be affected by this update.

I will discuss specific modifications of the function.php file in a future post on webspecialista.cz.

Step 5: Activate the child template

If you have followed the steps correctly, go to the Appearance section of > Templates in your site’s administration in the taskbar. In the list of installed templates, you should see the child template you created.

You activate it just like any other template, that is, by pressing the Activate button below the template preview image.

Once you activate a child template, it doesn’t mean you can delete the main template. Remember that the template you have now created is directly dependent on the existence of the master template.

 

A few words in conclusion

If you want to customize the current template on your site and the template itself doesn’t allow you to do so (for example, with a custom settings panel) or the Customize feature, the child template is the safest way to go.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top