The get_index_template() WordPress PHP function retrieves the path of the index template in the current or parent template.
Usage
get_index_template();
Parameters
- None
More information
See WordPress Developer Resources: get_index_template()
Examples
Retrieve the index template path
This code retrieves the index template path and stores it in a variable.
$template_path = get_index_template(); echo 'Index template path: ' . $template_path;
Load index template
This code loads the index template directly.
$template_path = get_index_template(); load_template($template_path);
Check if the index template exists
This code checks if the index template exists and returns a message accordingly.
$template_path = get_index_template();
if (file_exists($template_path)) {
echo 'Index template exists.';
} else {
echo 'Index template not found.';
}
Add a filter to change the index template path
This code adds a filter to change the index template path to a custom one.
add_filter('index_template_hierarchy', 'my_custom_index_template');
function my_custom_index_template($templates) {
$templates[] = 'custom-folder/index.php';
return $templates;
}
Add a custom function to the index template
This code adds a custom function that runs before loading the index template.
add_action('get_header', 'my_custom_function');
function my_custom_function() {
// Your custom function code here
}
$template_path = get_index_template();
load_template($template_path);