Using WordPress ‘get_home_template()’ PHP function

The get_home_template() WordPress PHP function retrieves the path of the home template in the current or parent template.

Usage

$template_path = get_home_template();
echo $template_path;

Parameters

  • No parameters required.

More information

See WordPress Developer Resources: get_home_template

Examples

Display Home Template Path

Display the path of the home template.

$template_path = get_home_template();
echo 'Home template path: ' . $template_path;

Load Home Template

Load the home template in a custom WordPress page.

$template_path = get_home_template();
load_template($template_path);

Check if Home Template Exists

Check if a home template exists and display a message accordingly.

$template_path = get_home_template();
if ($template_path) {
    echo 'Home template found!';
} else {
    echo 'Home template not found.';
}

Create a Backup Home Template

Create a backup of the home template file in the same directory.

$template_path = get_home_template();
$backup_path = dirname($template_path) . '/home-backup.php';
copy($template_path, $backup_path);

Use Home Template with Custom Query

Create a custom query and use the home template to display the results.

$args = array(
    'post_type' => 'post',
    'posts_per_page' => 5,
);
$custom_query = new WP_Query($args);
$template_path = get_home_template();
load_template($template_path);
wp_reset_postdata();