2026年5月20日
2026年5月20日
WordPressのカスタムタクソノミーのテンプレートを作る方法
はじめに
カスタムタクソノミー(分類)の一覧ページを正しく表示するには、テーマのテンプレート階層を理解し、適切なテンプレートファイルを作成する必要があります。タクソノミーページでは term_description() や get_terms() で一覧を取得できます。
症状・原因
- カスタムタクソノミーのターム一覧ページが正しく表示されない
- タクソノミー専用のテンプレートを作りたい
- タームごとにデザインを変えたい
- タクソノミーのすべてのタームを一覧表示したい
解決手順
ステップ1:カスタムタクソノミーを登録する
// functions.php
function mytheme_register_news_category(): void {
register_taxonomy('news_category', ['news'], [
'labels' => [
'name' => 'ニュースカテゴリ',
'singular_name' => 'ニュースカテゴリ',
'all_items' => 'すべてのカテゴリ',
'edit_item' => 'カテゴリを編集',
'add_new_item' => 'カテゴリを追加',
],
'public' => true,
'hierarchical' => true, // カテゴリ型(falseはタグ型)
'show_in_rest' => true,
'show_admin_column' => true,
'rewrite' => ['slug' => 'news-category'],
]);
}
add_action('init', 'mytheme_register_news_category');
ステップ2:テンプレート階層を理解する
WordPressのタクソノミーテンプレート優先順:
1. taxonomy-news_category-{term-slug}.php ← 特定ターム専用
2. taxonomy-news_category.php ← タクソノミー専用
3. taxonomy.php ← 全タクソノミー共通
4. archive.php
5. index.php
ステップ3:タクソノミーテンプレートを作成する
// taxonomy-news_category.php
get_header();
$term = get_queried_object(); // 現在のタームオブジェクト
?>
<main id="main" class="site-main">
<header class="page-header taxonomy-header">
<h1 class="page-title">
<?php echo esc_html($term->name); ?>
</h1>
<?php if ($term->description) : ?>
<div class="term-description">
<?php echo wp_kses_post(term_description()); ?>
</div>
<?php endif; ?>
<p class="term-count">
<?php printf('%d件の記事', $term->count); ?>
</p>
</header>
<?php if (have_posts()) : ?>
<div class="news-grid">
<?php while (have_posts()) : the_post(); ?>
<?php get_template_part('template-parts/card', 'news'); ?>
<?php endwhile; ?>
</div>
<?php the_posts_pagination(); ?>
<?php else : ?>
<p>この分類の記事はまだありません。</p>
<?php endif; ?>
</main>
<?php
get_footer();
ステップ4:サイドバーにタクソノミー一覧を表示する
// sidebar.php または ウィジェットテンプレート
$news_categories = get_terms([
'taxonomy' => 'news_category',
'hide_empty' => true, // 記事がないタームは非表示
'orderby' => 'name',
'order' => 'ASC',
]);
if (!empty($news_categories) && !is_wp_error($news_categories)) : ?>
<nav class="taxonomy-nav">
<h3>カテゴリ</h3>
<ul>
<?php foreach ($news_categories as $cat) : ?>
<li class="<?php echo is_tax('news_category', $cat) ? 'current-term' : ''; ?>">
<a href="<?php echo esc_url(get_term_link($cat)); ?>">
<?php echo esc_html($cat->name); ?>
<span class="count">(<?php echo $cat->count; ?>)</span>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
<?php endif;
ステップ5:投稿に付いたタクソノミーを表示する
// single-news.php またはカードテンプレート内
$terms = get_the_terms(get_the_ID(), 'news_category');
if ($terms && !is_wp_error($terms)) {
$term_links = array_map(function (WP_Term $term): string {
return sprintf(
'<a href="%s" class="term-link">%s</a>',
esc_url(get_term_link($term)),
esc_html($term->name)
);
}, $terms);
echo '<div class="post-terms">' . implode(' ', $term_links) . '</div>';
}
注意事項
- タクソノミーを登録した後、パーマリンク設定を再保存してリライトルールを更新してください
get_termsのhide_empty => falseにすると記事がないタームも取得できます。サイドバーなどではtrueが一般的ですis_tax('news_category', $term)で現在表示中のタームと一致するかを判定できます。ナビのアクティブ表示に使えます
まとめ
taxonomy-{taxonomy}.php でタクソノミー専用テンプレートを作り、get_queried_object() で現在のタームを取得します。get_terms() で全タームを取得してナビやサイドバーに一覧表示できます。get_term_link() でターム一覧ページへのURLを取得します。