How to load parent theme template parts in child theme

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.

Topic parent-theme child-theme functions template-hierarchy Wordpress

Category Web


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.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.