目的
- 投稿タイプ「投稿(Post)」の記事詳細ページを表示するテンプレート
single.phpを作成する - 投稿のタイトル・本文・日付などを出力し、1件ずつの詳細ページを実現する
- 投稿タイプ「投稿」専用テンプレートとして、固定ページ(
page.php)と区別する
投稿タイプ「投稿」と「固定ページ」の違い
| 種類 | ファイル | 用途 |
|---|---|---|
| 投稿 | single.php | ブログ記事などの「日付付きコンテンツ」用 |
| 固定ページ | page.php | お知らせやアクセスページなど、構造が変わらないページ用 |
single.php を作成する
single.php を新規作成し、以下のコードを記述します。
<?php get_header(); ?>
<!-- ===========================
ヘッダーを読み込み(header.php)
=========================== -->
<main class="single-post"> <!-- 投稿詳細ページ用のメインクラス -->
<!-- タイトルとメタ情報(投稿日時・カテゴリー) -->
<div class="sub-title">
<h1><?php the_title(); ?></h1> <!-- 投稿のタイトル -->
<div class="post-meta">
<!-- 投稿日時:datetime属性にフォーマット付きで -->
<time datetime="<?php echo get_the_date('Y-m-d'); ?>">
<?php echo get_the_date('Y年n月j日'); ?>
</time>
<!-- カテゴリー表示:複数ある場合はカンマ区切り -->
<span class="post-category">
<?php the_category(', '); ?>
</span>
</div>
</div>
<!-- メイン+サイドバーの2カラムレイアウト -->
<div class="container flex-area">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- 投稿1件分の内容を囲む -->
<article class="main-area">
<!-- アイキャッチ画像が設定されていれば表示 -->
<?php if (has_post_thumbnail()) : ?>
<div class="post-thumbnail">
<?php the_post_thumbnail('large'); ?> <!-- サイズはlarge指定 -->
</div>
<?php endif; ?>
<!-- 投稿本文 -->
<div class="post-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; endif; ?>
<!-- サイドバー -->
<aside class="sidebar">
<?php get_sidebar(); ?>
</aside>
</div>
</main>
<!-- フッターを読み込み -->
<?php get_footer(); ?>
投稿ページで使われている主なテンプレートタグとクラス
| タグ/クラス名 | 内容 | 解説例 |
|---|---|---|
the_time('Y-m-d') | 投稿日のHTML属性用フォーマット | <time datetime="2025-08-01"> などの日付属性に使われます。SEOにも重要です。 |
the_time('Y年n月j日') | 投稿日の表示用 | ページに表示される日本語形式の日付です(例:2025年8月1日)。 |
the_category(', ') | カテゴリー名の一覧を表示 | 投稿に設定されたカテゴリーをカンマ区切りで表示します。リンク付きで出力されます。 |
has_post_thumbnail() | アイキャッチ画像の有無を判定 | 投稿にアイキャッチが登録されていれば true を返し、表示処理に進みます。 |
the_post_thumbnail('large') | アイキャッチ画像を表示 | 登録された画像を large サイズで表示します。画像サイズはテーマによって異なります。 |
.post-meta | メタ情報表示用のクラス | 日付やカテゴリーなど、投稿に関する基本情報をまとめて装飾するためのクラスです。 |
.post-category | カテゴリー表示用のクラス | the_category() の出力にスタイルを適用するためのクラスです。 |
.post-thumbnail | アイキャッチ用のクラス | アイキャッチ画像の周りに余白や枠線をつけるなどの装飾に使います。 |
the_time()は2回使われており、1つは<time datetime>属性用、もう1つは画面表示用に分けています。has_post_thumbnail()を使うことで、アイキャッチの有無によってレイアウトが崩れるのを防ぎます。the_category()は<?php the_category(', '); ?>のように記述することで、複数カテゴリーがあっても適切に表示されます。
スタイルを追加(custom.css)
シンプルなCSSを追加しています。適宜変更可能です。
.sub-title {
background: var(--primary) url(images/renga.png) center/cover;
text-align: center;
padding: 3rem 0;
color: var(--bk-primary);
margin-bottom: 3rem;
}
.post-meta {
font-size: 0.9rem;
color: #666;
margin-bottom: 1rem;
}
.post-meta time {
margin-right: 1rem;
}
.post-category a {
color: var(--primary);
text-decoration: none;
}
.post-category a:hover {
text-decoration: underline;
}
.post-content {
line-height: 1.8;
font-size: 1rem;
}

