タイトル: get_header
SEOタイトル: WordPress get_header 関数の使い方(テーマヘッダ読み込み・$name 引数・$args パラメータ)
この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;background:#fffbea;border:1px solid #f4c430;margin:1em 0;width:100%"><tr><td style="background:#f4c430;color:#000;font-weight:bold;padding:8px">この記事の要点</td></tr><tr><td style="padding:10px"><ul><li><strong>get_header()</strong> はテーマの <code>header.php</code> を読み込む WordPress テンプレートタグ</li><li><strong>$name</strong> 引数で <code>header-{$name}.php</code> を読み込み — ページタイプ毎にヘッダを切り替えられる</li><li>WP 5.5+ では第 2 引数 <strong>$args</strong>(配列)でテンプレートに変数を渡せる</li><li>対象ファイルが見つからない場合は、コアの <code>wp-includes/theme-compat/header.php</code> がフォールバックとして読み込まれる</li><li><code>get_header_template()</code> フィルター / <code>get_header</code> アクションでヘッダ読み込み前後に処理を挿入可能</li><li>関連: <a href="https://jpn.itlibra.com/article/20888">get_footer 関数</a> / <a href="https://jpn.itlibra.com/article/20889">get_sidebar 関数</a></li></ul></td></tr></table>
<h2>get_header 関数とは</h2>
<p><strong>get_header()</strong> は WordPress のテンプレートタグで、現在有効なテーマの <code>header.php</code> ファイルを<strong>読み込んで出力</strong>します。ページごとに同じヘッダ(ロゴ・グローバルナビ・<head> タグなど)を再利用するのが目的で、ほぼすべてのテーマファイル冒頭で呼ばれます。</p>
<h2>構文</h2><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php">get_header( string $name = null, array $args = array() ): void|false</code></pre>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>引数</th><th>型</th><th>意味</th></tr></thead>
<tbody>
<tr><td>$name</td><td>string</td><td>読み込むヘッダ名。<code>header-{$name}.php</code> を探す。null なら <code>header.php</code></td></tr>
<tr><td>$args</td><td>array</td><td>テンプレートに渡す変数の連想配列(WP 5.5+)</td></tr>
</tbody>
</table>
<p>戻り値は <code>void</code>(通常)または <code>false</code>(テンプレート発見できず)。WP 5.5 以降は <code>void|false</code>。</p>
<h2>テンプレート読み込みの優先順位</h2>
<p><code>get_header('foo')</code> と呼ぶと WordPress は次の順で探します。</p>
<ol>
<li>子テーマの <code>header-foo.php</code></li>
<li>親テーマの <code>header-foo.php</code></li>
<li>子テーマの <code>header.php</code></li>
<li>親テーマの <code>header.php</code></li>
<li>コア <code>wp-includes/theme-compat/header.php</code>(フォールバック)</li>
</ol>
<p>子テーマでオーバーライドできる仕組みなので、子テーマ作成時はこの仕様を理解しておくと便利です。</p>
<h2>基本的な使い方</h2><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php"><?php
// 最も一般的な使い方(header.php を読み込む)
get_header();
?>
<main>
<?php while ( have_posts() ) : the_post(); ?>
<article>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php endwhile; ?>
</main>
<?php get_footer(); ?></code></pre>
<h2>ページ毎にヘッダを切り替える</h2>
<p>トップページとサブページでヘッダを変えたい場合、<code>$name</code> 引数を渡します。</p><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php"><?php
// front-page.php
get_header( 'front' ); // header-front.php を読み込む
?>
<?php
// page.php
get_header(); // header.php を読み込む
?>
<?php
// single.php(記事ページ)
get_header( 'single' ); // header-single.php を読み込む
?></code></pre>
<h2>$args で変数を渡す(WP 5.5+)</h2><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php"><?php
// テンプレートを呼ぶ側
get_header( null, array(
'class' => 'home-hero',
'show_search' => true,
) );
?>
<!-- header.php 内 -->
<header class="<?php echo esc_attr( $args['class'] ?? '' ); ?>">
<?php if ( $args['show_search'] ?? false ) : ?>
<?php get_search_form(); ?>
<?php endif; ?>
</header></code></pre>
<p><code>$args</code> の中身はテンプレート内で <code>$args</code> 変数として参照できます。</p>
<h2>典型的な header.php の中身</h2><pre style="background:#1e293b;color:#e2e8f0;padding:1em;border-radius:6px;overflow-x:auto;line-height:1.5"><code class="language-php"><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title><?php wp_title(); ?></title>
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
<header class="site-header">
<h1 class="site-title">
<a href="<?php echo esc_url( home_url( '/' ) ); ?>">
<?php bloginfo( 'name' ); ?>
</a>
</h1>
<?php
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_class' => 'main-nav',
) );
?>
</header></code></pre>
<p><code>wp_head()</code> はプラグインやコアが head 内に出力するためのフックなので、テーマには必須。</p>
<h2>関連フック</h2>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>フック</th><th>種類</th><th>用途</th></tr></thead>
<tbody>
<tr><td>get_header</td><td>action</td><td>get_header() 実行時、テンプレート読み込み前に発火</td></tr>
<tr><td>get_header_template</td><td>filter</td><td>読み込まれるテンプレートのパスを変更</td></tr>
<tr><td>wp_head</td><td>action</td><td>header.php 内の wp_head() で発火(CSS / JS 出力)</td></tr>
<tr><td>wp_body_open</td><td>action</td><td>body 開始直後(GA タグ・Cookie 同意など)</td></tr>
</tbody>
</table>
<h2>注意点</h2>
<ul>
<li><strong>functions.php で呼ばない</strong> — 初期化前は wp_head 等が動かない。テンプレート (single.php / page.php) の冒頭で呼ぶ</li>
<li>$name はファイル名の <code>-</code> 以降。<code>header-mobile.php</code> なら <code>get_header('mobile')</code></li>
<li>ヘッダ内で大きな処理を行うとページ全体の生成が遅くなる — 重い処理はキャッシュ化</li>
</ul>
<h2>関連関数</h2>
<table border="1" cellpadding="6" cellspacing="0" style="border-collapse:collapse;width:100%">
<thead><tr style="background:#e3f2fd"><th>関数</th><th>役割</th></tr></thead>
<tbody>
<tr><td><a href="https://jpn.itlibra.com/article/20888">get_footer()</a></td><td>footer.php を読み込む</td></tr>
<tr><td><a href="https://jpn.itlibra.com/article/20889">get_sidebar()</a></td><td>sidebar.php を読み込む</td></tr>
<tr><td>get_template_part()</td><td>任意のテンプレート部品を読み込む</td></tr>
<tr><td>locate_template()</td><td>テンプレートのパスを取得(読み込まずに)</td></tr>
</tbody>
</table>
<h2>FAQ</h2>
<p><strong>Q: get_header() を呼ばないとどうなる?</strong><br />
A: wp_head() フックが動かず、プラグインが出力する CSS/JS が読み込まれない。トラッキングや表示も壊れる。</p>
<p><strong>Q: 同じテンプレートで複数回呼んでいい?</strong><br />
A: 技術的には可能だが、<html> / <head> を二重出力する事故になるので非推奨。</p>
<p><strong>Q: ブロックテーマ(FSE)でも使う?</strong><br />
A: ブロックテーマでは theme.json + block templates が中心で、PHP テンプレートと get_header() は使わない。クラシックテーマでのみ必須。</p>
<p>関連: <a href="https://jpn.itlibra.com/article/20888">get_footer 関数</a> / <a href="https://jpn.itlibra.com/article/20889">get_sidebar 関数</a></p>