【WordPressインクルードタグ】get_template_part():テンプレートパーツを読み込む
概要
パラメーターで指定したテンプレートパーツ(ヘッダー、サイドバー、フッター以外) を読み込む。
パラメータは2つ指定可能で、最初のパラメーターは必須。
例えば、get_template_part('content' , 'index'); の場合、content-index.phpがあれば、content-index.phpを読み込み、なければcontent.phpを読み込む。
書式
パラメーター
- slug
- ファイル名(必須)(初期値:null)
※[slug].php を挿入する。
※記事のスラッグとは無関係。
- name
- サブファイル名(初期値:null)
※指定した場合は[slug]-[name].phpを読み込む。[slug]-[name].phpがなければ、[slug].phpを読み込む。
使用例
初期設定での使用
「content.php」を読み込む
<?php get_template_part('content'); ?>
「archive.php」を読み込む
<?php get_template_part('archive'); ?>
サブファイル名を指定して使用
「content-index.php」を読み込む。一部のページで部分的にパーツを変えるときに便利。
<?php get_template_part('content' , 'index'); ?>
複数ヘッダーの指定(具体例)
トップページは「content-index.php」を読み込み、エラーページ(404.php)のときは「content-404.php」を読み込み、それ以外のページは「content.php」を読み込むよう指定する。
<?php if ( is_home() ) : get_template_part('content' 'index'); elseif ( is_404() ) : get_template_part('content' ,'404'); else : get_template_part('content'); endif; ?>
関連
- wordpressのテンプレートタグ一覧
- wordpressのインクルードタグ一覧
- get_header()
- get_footer()
- get_sidebar()
- get_search_form()
- comments_template()
関連記事
-
-
【WordPressテンプレートタグ】get_post_thumbnail_id():アイキャッチ画像のIDを取得
概要 アイキャッチ画像のIDを取得する。 書式 <?php get_post_thumbnai...
-
-
【WordPressテンプレートタグ】edit_comment_link():コメント編集画面へのリンクを表示
概要 コメント編集画面へのリンクを表示する。 書式 <?php edit_comment_li...
-
-
【WordPressインクルードタグ】get_footer():「footer.php」を読み込む
概要 テンプレートファイル「footer.php」を読み込む 書式 <?php get_foo...
-
-
【WordPressインクルードタグ】get_header():「header.php」を読み込む
概要 テンプレートファイル「header.php」を読み込む 書式 <?php get_hea...
-
-
【WordPressテンプレートタグ】get_calendar():日別アーカイブへのリンク付きカレンダーを表示または取得
概要 投稿のあった日を日別アーカイブへのリンクとしたカレンダーを表示、または文字列として取得する。 ...
-
-
【WordPressテンプレートタグ】next_posts_link():次のアーカイブへのリンクを表示
概要 記事一覧ページ(メインページ・アーカイブページ・カテゴリーなど)の次ページ(日付の古い記事一覧...
-
-
【WordPressテンプレートタグ】get_the_date():記事の公開日を取得
概要 現在の記事の公開日を取得する。 書式 <?php get_the_date('forma...
-
-
【WordPressテンプレートタグ】get_edit_post_link():記事やページの編集画面へのURLを取得
概要 現在の記事やページの編集画面へのURLを取得する。wordpressにログインしていない場合は...
-
-
【WordPressテンプレートタグ】get_the_post_thumbnail():アイキャッチ画像を取得
概要 アイキャッチ画像のimgタグを取得する。 書式 <?php get_the_post_t...
-
-
【WordPressテンプレートタグ】has_post_thumbnail():アイキャッチ画像が登録されているかを判別
概要 アイキャッチ画像が登録されているかどうかを判別する。 書式 <?php has_post...