// defining the sub-directory so that it can be easily accessed from elsewhere as well.
define( 'WPSE_PAGE_TEMPLATE_SUB_DIR', 'page-singaku' );
function wpse312159_page_template_add_subdir( $templates = array() ) {
// Generally this doesn't happen, unless another plugin / theme does modifications
// of their own. In that case, it's better not to mess with it again with our code.
if( empty( $templates ) || ! is_array( $templates ) || count( $templates ) < 3 )
return $templates;
$page_tpl_idx = 0;
if( $templates[0] === get_page_template_slug() ) {
// if there is custom template, then our page-{slug}.php template is at the next index
$page_tpl_idx = 1;
}
$page_tpls = array( WPSE_PAGE_TEMPLATE_SUB_DIR . '/' . $templates[$page_tpl_idx] );
// As of WordPress 4.7, the URL decoded page-{$slug}.php template file is included in the
// page template hierarchy just before the URL encoded page-{$slug}.php template file.
// Also, WordPress always keeps the page id different from page slug. So page-{slug}.php will
// always be different from page-{id}.php, even if you try to input the {id} as {slug}.
// So this check will work for WordPress versions prior to 4.7 as well.
if( $templates[$page_tpl_idx] === urldecode( $templates[$page_tpl_idx + 1] ) ) {
$page_tpls[] = WPSE_PAGE_TEMPLATE_SUB_DIR . '/' . $templates[$page_tpl_idx + 1];
}
array_splice( $templates, $page_tpl_idx, 0, $page_tpls );
return $templates;
}
add_filter( 'page_template_hierarchy', 'wpse312159_page_template_add_subdir' );
function tbdn_get_page_template() {
$id = get_queried_object_id();
$template = get_page_template_slug();
$pagename = get_query_var('pagename');
if ( ! $pagename && $id ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = get_queried_object();
if ( $post )
$pagename = $post->post_name;
}
$templates = array();
if ( $template && 0 === validate_file( $template ) )
$templates[] = $template;
// if there's a custom template then still give that priority
if ( $pagename )
$templates[] = "page-shingaku/page-$pagename.php";
if ( $pagename )
$templates[] = "toshin-net/page-$pagename.php";
if ( $pagename )
$templates[] = "believe/page-$pagename.php";
// change the default search for the page-$slug template to use our directory
// you could also look in the theme root directory either before or after this
if ( $id )
$templates[] = "page-shingaku/page-$id.php";
$templates[] = 'page.php';
if ( $id )
$templates[] = "toshin-net/page-$id.php";
$templates[] = 'page.php';
if ( $id )
$templates[] = "believe/page-$id.php";
$templates[] = 'page.php';
/* Don't call get_query_template again!!!
// return get_query_template( 'page', $templates );
We also reproduce the key code of get_query_template() - we don't want to call it or we'll get stuck in a loop .
We can remove lines of code that we know won't apply for pages, leaving us with...
*/
$template = locate_template( $templates );
return $template;
}
add_filter( 'page_template', 'tbdn_get_page_template' );