2

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?

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.

2

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; 
}
  • I haven't tested it yet but it looks right. Thanks! – cwd Jun 30 '11 at 3:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.