Adjust which tempalte a page uses with a function?

I know if I had a page that had a permalink such as /stories/ then I could create a page in my template called page-stores.php and it would load in place of the default page.php when that page loaded (see Template Hierarchy)

What I want to know is if I had that two page templates, maybe page.php and page2.php, if I could use the functions.php file to specify that if a page had a permalink of /stories/ it would use page2.php?

Read More

I know there is an option when editing a page to let the end user choose the page template, but in this case I would like to set it specifically.

Related posts

Leave a Reply

1 comment

  1. This might work:

    add_filter('page_template', 'custom_page_template');
    
    function custom_page_template($template){
      // check your permalink here
      if(get_query_var('pagename') === 'stories')
        return locate_template(array('page2.php', 'page.php')); 
    
      return $template; 
    }