テンプレートとテンプレートパーツの組み合わせを考える

WordPressのテーマを作るとき、「固定ページ」用のテンプレートと「個別投稿ページ」用のテンプレートってどうしてますか?
ほとんど同じ構造だと思いませんか?
管理画面の入力フォームを見ても「タイトル」と「本文」がメインですし…。
違いといえば、「個別投稿ページ」には、meta情報(投稿日、カテゴリ、タグ、執筆者)を表示する必要があることが多いぐらいじゃないでしょうか?
「固定ページ」「個別投稿ページ」のテンプレートとテンプレートパーツの組み合わせを考えてみました。

テンプレートとテンプレートパーツの組み合わせのパターン

考えられる組み合わせを表にしました。

テンプレートテンプレートパーツ
1page.php
single.php
なし
2page.php
single.php
content.php
3singular.phpcontent-page.php
content-.single.php
4singular.phpcontent.php
5singular.phpなし

1.テンプレートだけを使う

「固定ページ」「個別投稿ページ」のテンプレートにしっかり記述する方法です。
それぞれ独立しているので、パッ見てわかりやすいかもしれません。
しかし、変更・修正があったときに、2ファイルを扱わないといけなくなるので、漏れが起こる可能性があります。

■page.php
<?php while(have_posts()):?>
	<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">
			<?php the_content();?>
		</div>
	</article>
<?php endwhile;?>
■single.php
<?php while(have_posts()):?>
	<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-meta">
			<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
			<?php the_category();?>
			<?php the_tags(',');?>
		</div>
		<div class="entry-content">
			<?php the_content();?>
		</div>
	</article>
<?php endwhile;?>

※「single.php」には「entry-meta」のブロックが存在しますが、「page.php」と内容はほぼ同じです。

2.共通テンプレートパーツ内で表示する内容を切り替える

「single.php」「page.php」には共通部分が多いので、共通のテンプレートパーツを作ります。
その中で、条件分岐タグ「is_single()」を使って、表示する内容を切り替えます。

■page.php / single.php
<?php while(have_posts()):
	get_template_part('content');
endwhile;?>
■content.php
<article id="post-<?php the_ID();?>" <?php post_class();?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title();?></h1>
	</header>
	<?php if(is_single())://個別投稿ページだったら?>
	<div class="entry-meta">
		<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
		<?php the_category();?>
		<?php the_tags(',');?>
	</div>
	<?php endif;?>
	<div class="entry-content">
		<?php the_content();?>
	</div>
</article>

3.読み込むテンプレートパーツを切り替えて表示する

「page.php」と「single.php」には、共通のテンプレートファイル「singular.php」が存在します。
この「singular.php」内で、条件分岐タグ「is_page()」「is_single()」を使って、読み込むテンプレートパーツを変更してみます。

■singular.php
<?php while(have_posts()):
	if(is_page())://固定ページのとき、または「is_single()」個別投稿ページのとき
		get_template_part('content-page');
	else:
		get_template_part('content-single');
	endif;
endwhile;?>
■content-page.php
<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">
		<?php the_content();?>
	</div>
</article>
■content-single.php
<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-meta">
		<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
		<?php the_category();?>
		<?php the_tags(',');?>
	</div>
	<div class="entry-content">
		<?php the_content();?>
	</div>
</article>

4.共通テンプレートに共通テンプレートパーツで表示内容を切り替える

「2.共通テンプレートパーツ内で表示する内容を切り替える」と「3.読み込むテンプレートパーツを切り替えて表示する」の合わせ技みたいな方法です。
共通テンプレートに共通テンプレートパーツを読み込みます。

■singular.php
<?php while(have_posts()):
	get_template_part('content');
endwhile;?>
■content.php
<article id="post-<?php the_ID();?>" <?php post_class();?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title();?></h1>
	</header>
	<?php if(is_single())://個別投稿ページだったら?>
	<div class="entry-meta">
		<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
		<?php the_category();?>
		<?php the_tags(',');?>
	</div>
	<?php endif;?>
	<div class="entry-content">
		<?php the_content();?>
	</div>
</article>

5.共通テンプレートパーツ内で表示する内容を切り替える

if文を使うなら、わざわざファイルを分ける必要なんてないと考えることもできます。
その方法が共通テンプレート「singular.php」内で完結させるやり方です。

■singular.php
<?php while(have_posts()):?>
	<article id="post-<?php the_ID();?>" <?php post_class();?>>
		<header class="entry-header">
			<h1 class="entry-title"><?php the_title();?></h1>
		</header>
		<?php if(is_single())://個別投稿ページだったら?>
			<div class="entry-meta">
				<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
				<?php the_category();?>
				<?php the_tags(',');?>
			</div>
		<?php endif;?>
		<div class="entry-content">
			<?php the_content();?>
		</div>
	</article>
<?php endwhile;?>

ユニークな部分をパーツ化する

パーツ化というと共通部分を別ファイルにするイメージがありますが、逆に、ユニークな部分をパーツ化して、必要なときにだけ読み込ませるという考え方もできます。

この場合「<div=”entry-meta”>~</div>」の部分です。
そこで、「entry-meta.php」というテンプレートパーツを作って、「個別投稿ページ」というか「投稿日」などのメタ情報を表示したいとき、読み込ませることもできます。

■entry-meta.php
<div class="entry-meta">
	<time class="entry-date published" datetime="<?php echo esc_attr(get_the_date());?>"><?php the_date();?></time>
	<?php the_category();?>
	<?php the_tags(',');?>
</div>

テンプレートパーツの中で、さらにテンプレートパーツを読み込ませることもできますので、「content.php」や「content-single.php」でも「entry-meta.php」をパーツ化しても問題ありません。

■content.php
<article id="post-<?php the_ID();?>" <?php post_class();?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title();?></h1>
	</header>
	<?php if(is_single())://個別投稿ページだったら
		get_template_part('entry-meta');
	endif;?>
	<div class="entry-content">
		<?php the_content();?>
	</div>
</article>
■content-single.php
<article id="post-<?php the_ID();?>" <?php post_class();?>>
	<header class="entry-header">
		<h1 class="entry-title"><?php the_title();?></h1>
	</header>
	<?php get_template_part('entry-meta');endif;?>
	<div class="entry-content">
		<?php the_content();?>
	</div>
</article>

「entry-meta.php」をパーツ化することで、組み合わせのパターンはさらに増えることになります。

組み合わせのパターン

テンプレートテンプレートパーツ
1page.php
single.php
なし
2page.php
single.php
content.php
3page.php
single.php
content.php
entry-meta.php
4singular.phpcontent-page.php
content-.single.php
5singular.phpcontent-page.php
content-.single.php
entry-meta.php
6singular.phpcontent.php
7singular.phpcontent.php
entry-meta.php
8singular.phpなし

テンプレートとテンプレートパーツのまとめ

テーマのファイル構成は自由です。
どの方法でも間違いではありません。
自由過ぎて、何が正しいかわからなくなることもあります。
ただ、共通部分の多いファイル数が複数できてしまう方法は、作業し忘れる可能性が生じるため、オススメはしないです。
自分の管理しやすい構成にすることが大事です。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です