Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created April 19, 2016 20:03
Show Gist options
  • Select an option

  • Save chrisguitarguy/894fbd8e982bd80a55446bf8b822e710 to your computer and use it in GitHub Desktop.

Select an option

Save chrisguitarguy/894fbd8e982bd80a55446bf8b822e710 to your computer and use it in GitHub Desktop.
<?php
add_filter('page_template', function ($template) {
$page = get_queried_object();
$slugs = [];
do {
$slugs[] = $page->post_name;
$page = empty($page->post_parent) ? null : get_post($page->post_parent);
} while ($page);
$name = sprintf('pages/%s.php', implode('/', array_reverse($slugs)));
if ($_template = locate_template($name)) {
$template = $_template;
}
return $template;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment