I was wondering how can I load parent theme template parts through child theme after customization. I have created three custom template for my theme and they are working fine. But now I want to load parent theme template parts layout. For example if I want edit profile page the location of the PHP file in parent theme is template_parts/layouts/profile/profile-modern.php In that folder there are other parts profile-header.php and other files. I tried to edit the profile-mordern.php directly in the parent theme and it works. But after theme update, it will be gone so I want to load that file from my child theme. I tried to search the solution but didn't get any specifically. Please can anyone help me regarding this issue? And I am learning PHP programming and WordPress theme customization so please guide me step by step if possible. And also resource link to learn.
2 Answers
From the WordPress documentation on child themes (Referencing or Including Other Files):
To reference the parent theme directory, you would use get_template_directory() instead.
Therefore, in your child theme, you could include your parent template file like this:
<?php
include get_template_directory() . '/template_parts/layouts/profile/profile-modern.php';
?>
For every theme file present in the parent directory, WordPress will check whether a corresponding file is present in the child theme and, if so, use that one instead. This means that a profile-modern.php file in the child theme will override its equivalent in the parent folder.
So, if you don’t like something about a page’s layout, just copy the respective file, implement your changes, and upload it to the child theme’s folder. The modifications will then appear in the child theme, while the original file will remain untouched.
-
-
1"For every theme file present in the parent directory" This is not correct. This only applies to certain files and functions. WordPress itself only checks for template files in the template hierarchy and some other partial templates. For a child theme to load other files the parent must use certain functions. For example,
get_template_part()
will search the child theme, butinclude
will not. Neither willget_stylesheet_directory_uri()
, butget_them_file_uri();
will. Apr 24, 2020 at 7:12 -
-
If your parent theme uses
get_template_part()
correctly, then just copy the files into the same folder in your child theme. If that doesn't work it might not be possible. You should check with the theme author. Apr 24, 2020 at 13:42