2026年5月20日
2026年5月20日
WordPressのテンプレート階層を理解して正しく使う方法
はじめに
WordPressはページの種類(投稿・固定ページ・アーカイブ・404など)に応じて、テーマ内のどのPHPファイルを使うかを「テンプレート階層」というルールで決定します。この階層を理解すると、URLやページの種類に応じたカスタムデザインを簡単に実装できます。
症状・原因
- 特定のページだけデザインを変えたい
- どのPHPテンプレートが使われているか確認したい
- カスタムページテンプレートの作り方がわからない
- 投稿タイプやスラッグ別にテンプレートを分けたい
解決手順
ステップ1:主要なテンプレート階層を理解する
固定ページ(Page)の階層:
1. {template-name}.php (カスタムページテンプレート)
2. page-{slug}.php (スラッグ別)
3. page-{id}.php (ID別)
4. page.php
5. singular.php
6. index.php
投稿(Single Post)の階層:
1. single-{post-type}-{slug}.php
2. single-{post-type}.php
3. single.php
4. singular.php
5. index.php
アーカイブの階層:
1. archive-{post-type}.php
2. archive.php
3. index.php
タクソノミーの階層:
1. taxonomy-{taxonomy}-{term}.php
2. taxonomy-{taxonomy}.php
3. taxonomy.php
4. archive.php
5. index.php
ステップ2:現在使われているテンプレートを確認する
// functions.php: 開発時のテンプレート確認(管理者のみ)
function mytheme_show_template(): void {
if (!is_admin() && current_user_can('administrator')) {
global $template;
echo '<div style="position:fixed;bottom:0;right:0;background:#1d2327;color:#a7aaad;padding:8px 12px;font-size:11px;z-index:9999;">';
echo 'Template: ' . esc_html(basename($template));
echo '</div>';
}
}
add_action('wp_footer', 'mytheme_show_template');
または Query Monitor プラグインを使うとテンプレートパスを確認できます。
ステップ3:スラッグ別・ID別のテンプレートを作る
// ページスラッグ「about」専用テンプレート: page-about.php
// ページID 42専用テンプレート: page-42.php
// 投稿タイプnewsのスラッグ「first-news」: single-news-first-news.php
// page-about.php の例
get_header();
?>
<main id="main" class="site-main page-about">
<section class="about-hero">
<h1><?php the_title(); ?></h1>
</section>
<section class="about-content">
<?php
while (have_posts()) : the_post();
the_content();
endwhile;
?>
</section>
</main>
<?php get_footer();
ステップ4:カスタムページテンプレートを作成する
<?php
/**
* Template Name: フルワイドレイアウト
* Template Post Type: page, post
*/
// ↑ このコメントがテンプレートとして認識される条件
// Template Name: に名前を書くと管理画面の「テンプレート」ドロップダウンに表示される
get_header();
?>
<main id="main" class="site-main full-width-layout">
<?php
while (have_posts()) : the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content full-width">
<?php the_content(); ?>
</div>
</article>
<?php
endwhile;
?>
</main>
<?php get_footer();
配置場所:
- テーマルート直下:
your-theme/full-width.php - サブフォルダ:
your-theme/page-templates/full-width.php
ステップ5:PHPで動的にテンプレートを切り替える
// functions.php: 条件に応じてテンプレートを差し替える
function mytheme_custom_template(string $template): string {
// VIPユーザーには専用テンプレートを使用
if (is_singular('post') && current_user_can('vip_member')) {
$vip_template = get_template_directory() . '/single-vip.php';
if (file_exists($vip_template)) {
return $vip_template;
}
}
// カテゴリ「premium」の投稿には別テンプレート
if (is_singular('post') && has_category('premium')) {
$premium_template = get_template_directory() . '/single-premium.php';
if (file_exists($premium_template)) {
return $premium_template;
}
}
return $template;
}
add_filter('template_include', 'mytheme_custom_template');
注意事項
- カスタムページテンプレートのファイルは テーマフォルダ内 に置く必要があります。子テーマに置くことも可能です
Template Name:コメントは必ずPHPファイルの先頭(の直後)に書いてくださいtemplate_includeフィルターはテンプレートのフルパスを操作するため、file_exists()で存在確認してから返すようにしてください
まとめ
WordPressはテンプレート階層の順番でPHPファイルを探し、最初に見つかったものを使います。スラッグ別(page-{slug}.php)・ID別(page-{id}.php)・カスタムテンプレート(Template Name: コメント)を活用してページごとのデザインを実現します。動的な切り替えは template_include フィルターで行います。