ページテンプレートの読み込み方法:
デフォルトのWordPressテンプレート階層によると、pageリクエストは、以下のように優先度と命名に基づいてテンプレートをロードします。
Custom Page Template:ページエディタで定義されている場合。
page-{slug}.php
page-{url-encoded-slug}.php:マルチバイト文字のみ。
page-{id}.php
page.php
singular.php
index.php
これらのうち、singular.phpとindex.phpされていない、実際にページテンプレート。singular.phpは単一の投稿タイプindex.php用のフォールバックテンプレートであり、WordPressテンプレートがロードするはずのあらゆるもののための究極のフォールバックテンプレートです。したがって、最初の5つはページテンプレートです。
階層内のサブディレクトリからテンプレートファイルを挿入する方法:
WordPressコア関数get_page_template()は必要なpageテンプレート階層配列を生成し、階層からロードするテンプレートファイルを正確に決定する直前に、WordPressはpage_template_hierarchyフィルターフックを起動します。したがって、WordPressがpage-{slug}.phpテンプレートを自動的に検索するサブディレクトリを追加する最良の方法は、このフィルターを使用して、ページテンプレート階層配列内のそのサブディレクトリに関連する適切なファイル名を挿入することです。
注:元のフィルターフックはとして定義された動的フィルターフック{$type}_template_hierarchyで、wp-includes/template.phpファイルにあります。だから、とき$typeでpage、フィルタフックはなりpage_template_hierarchy。
ここで、目的のために、フックコールバック関数に渡されるテンプレート階層配列内のsub-directory/page-{slug}.php直前にファイル名を挿入しますpage-{slug}.php。そうsub-directory/page-{slug}.phpすれば、WordPressはファイルが存在する場合はそれをロードし、そうでない場合は通常のページテンプレートのロード階層に従います。もちろん、一貫性を維持するためCustom Page Templateに、sub-directory/page-{slug}.phpファイルと比較して優先度を高くします。したがって、変更されたページテンプレートの階層は次のようになります。
Custom Page Template:ページエディタで定義されている場合。
sub-directory/page-{slug}.php
sub-directory/page-{url-encoded-slug}.php:マルチバイト文字のみ。
page-{slug}.php
page-{url-encoded-slug}.php:マルチバイト文字のみ。
page-{id}.php
page.php
サンプルfunctions.phpコード:
この変更を1つのテーマでのみ行う場合は、アクティブなテーマのfunctions.phpファイルで次のコードを使用できます。
// defining the sub-directory so that it can be easily accessed from elsewhere as well.
define( 'WPSE_PAGE_TEMPLATE_SUB_DIR', 'page-templates' );
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' );
サンプルプラグイン:
複数のテーマで同じテンプレートファイルの編成に従う場合は、この機能をテーマとは別にしておくことをお勧めします。その場合、functions.php上記のサンプルコードでテーマのファイルを変更する代わりに、同じサンプルコードで単純なプラグインを作成する必要があります。
page-slug-template-subdir.phpWordPress pluginsディレクトリ内などに、次のコードをファイル名で保存します。
<?php
/*
Plugin Name: WPSE Page Template page-slug.php to Sub Directory
Plugin URI: https://wordpress.stackexchange.com/a/312159/110572
Description: Page Template with page-{slug}.php to a Sub Directory
Version: 1.0.0
Author: Fayaz Ahmed
Author URI: https://www.fayazmiraz.com/
*/
// defining the sub-directory so that it can be easily accessed from elsewhere as well.
define( 'WPSE_PAGE_TEMPLATE_SUB_DIR', 'page-templates' );
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] );
uded 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;
}
// the original filter hook is {$type}_template_hierarchy,
// wihch is located in wp-includes/template.php file
add_filter( 'page_template_hierarchy', 'wpse312159_page_template_add_subdir' );
使用法:
上記のコードのいずれかを使用すると、WordPressはテーマのディレクトリpage-{slug}.php内のテンプレートファイルをpage-templates自動的に認識します。
たとえば、aboutページがあるとします。したがって、custom page templateエディターからのセットTHEME/page-templates/page-about.phpがない場合、WordPressはTHEME/page-about.phpテンプレートファイルを探し、それが存在しない場合、WordPressはテンプレートファイルなどを探します(つまり、デフォルトのページテンプレート階層)。