Locating and using custom templates in WordPress with locate_template

The locate_template function in WordPress is used to locate and load a specific template file within a theme. This function is useful for developers who want to customize the appearance of their WordPress site by creating custom template files. It allows them to easily override default template files without modifying the core theme files.

By using the locate_template function, developers can organize their template files in a structured manner and ensure that the correct template file is loaded based on specific conditions or criteria. This can help to improve the maintainability and flexibility of the theme, as well as make it easier to manage and update in the future.

Parameters accepted by the WordPress locate_template function:

  • $template_names (string array, required): Template file(s) to search for, in order.
  • $load (boolean, optional, default value: false): If set to true, the template file will be loaded if it is found.
  • $load_once (boolean, optional, default value: true): Determines whether to require_once or require. This parameter has no effect if $load is set to false.
  • $args (array, optional, default value: array()): Additional arguments passed to the template.

Value returned by the WordPress locate_template function:

The function returns a string, which is the template filename if one is located.

Examples

How to use locate_template to load a specific template file

Here’s a simple example of using locate_template to load a specific template file:

<?php
 locate_template( 'template-parts/content.php', true, false );
?>

This code snippet uses the locate_template function to load the content.php file from the template-parts directory. The second parameter is set to true to require the template, and the third parameter is set to false to not load the default template.

How to use locate_template with a fallback template

Here’s an example of using locate_template with a fallback template:

<?php
 locate_template( array( 'template-parts/content-special.php', 'template-parts/content.php' ), true );
?>

In this code snippet, the locate_template function is used with an array of template file names. It will first attempt to load content-special.php, and if that file does not exist, it will fallback to loading content.php.

How to use locate_template with a conditional check

Here’s an example of using locate_template with a conditional check:

<?php
 if ( is_single() ) {
 locate_template( 'template-parts/content-single.php', true );
 } else {
 locate_template( 'template-parts/content.php', true );
 }
?>

This code snippet uses the locate_template function within an if-else statement to load different template files based on whether the current page is a single post or not.

Conclusion

In conclusion, the locate_template function is a crucial tool for developers working with WordPress themes. By providing a way to locate and load template files in a flexible and efficient manner, it allows for greater customization and organization within a theme. Understanding how to use this function effectively can greatly improve the development process and result in more maintainable and scalable code. With its ability to handle fallbacks and prioritize template files, locate_template is a valuable asset for any WordPress theme developer.

Related WordPress Functions