How To Override The WordPress Template Hierarchy

Template hierarchy is the way in which WordPress decides which template file will be used to display various pages in your website. In this post, we are going take a look at a way to override this hierarchy. To do this, we are going to use template_include filter.

Quick Tip : How to Send HTML formatted emails from WordPress?

add_filter( 'template_include', 'portfolio_page_template', 99 );

function portfolio_page_template( $template ) {

	if ( is_page( 'portfolio' )  ) {
		$new_template = locate_template( array( 'portfolio-template.php' ) );
		if ( '' != $new_template ) {
			return $new_template ;

	return $template;

As you can see, we are hooking into template_include filter. First we are using if condition to check if page with slug portfolio is being displayed. If the condition is satisfied we will define a new highest priority template using locate_template method. If the file is located we are going to return the file name stored inside $new_template variable.

Note: If locate_template function is able to locate the file, it will return the file name. Else, it will return an empty string.


Leave a Comment.