Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/locate template


Retrieve the name of the highest priority template file that exists, optionally loading that file.

Searches STYLESHEETPATH before TEMPLATEPATH, so themes which inherit from a parent can overload one file.


<?php locate_template$template_names$load$require_once ?>


(array) (required) Array of template files to search for, in priority order. (Requires file extension.)
Default: None
(boolean) (optional) If true, the template file will be loaded, if it is found.
Default: false
(boolean) (optional) If true, the template file will be loaded with the php require_once function. If false, the template file will be loaded with the php require function. This parameter has no effect if $load is false.
Default: true

Return Values

The template filename, if one is located, an empty string, if not.


Load a specific template part based on the current pagename:

if (locate_template( array( 'content-' . $pageName . '.php' ) ) != '') {
	// yep, load the page template
	get_template_part('content', $pageName);
} else {
	// nope, load the content


Change Log

Since: 2.7.0

Source File

locate_template() is located in wp-includes/template.php.


See also index of Function Reference and index of Template Tags.
This page is marked as incomplete. You can help Codex by expanding it.