インクルードタグ

Sponsored Link

テンプレートインクルードタグは、1つのテンプレートファイル(例:index.php)内で、別のテンプレートファイル(例header.php)内に含まれている、HTMLとPHPを実行するために使用されます。PHPは、include()内で構築する目的のためのステートメントがありますが、WordPressのテンプレートタグは、より簡単に特定のファイルをインクルードする事ができます。

Sponsored Link

原文(翻訳元)The Template include tags are used within one Template file (for example index.php) to execute the HTML and PHP found in another template file (for example header.php). PHP has a built in include() statement for this purpose, but these WordPress template tags make including certain specific files much easier.

テンプレートとテーマの詳細については、テーマとテーマ開発を参照してください。

原文(翻訳元)See Using Themes and Theme Development for more information about Templates and Themes.

ファンクションリファレンス

一般的なファイルをインクルード

その他のコンポーネントをインクルード

Headerテンプレート

<?php get_header(); ?>

get_header()タグは、現在のテーマのディレクトリからheader.phpファイルをインクルードします。ファイルが見つからない場合は、代わりにwp-content/themes/default/header.php.をインクルードします。

原文(翻訳元)The get_header() tag includes the file header.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/header.php.

Footerテンプレート

<?php get_footer(); ?>

get_footer()タグは、現在のテーマディレクトリからfooter.phpファイルをインクルードします。ファイルが見つからない場合は、代わりにwp-content/themes/default/footer.php.をインクルードします。

原文(翻訳元)The get_footer() tag includes the file footer.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/footer.php.

Sidebarテンプレート

<?php get_sidebar(); ?>

get_sidebar()タグは、現在のテーマディレクトリからsidebar.phpファイルをインクルードします。ファイルが見つからない場合は、代わりにwp-content/themes/default/sidebar.phpをインクルードします。

原文(翻訳元)The get_sidebar() tag includes the file sidebar.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/sidebar.php.

カスタムテンプレートファイル

<?php get_template_part(); ?>

get_template_part()タグは、header,sidebar,footer以外のカスタムインクルードタグである、現在のテーマディレクトリから{slug}.phpファイルをインクルードします。

原文(翻訳元)The get_template_part() tag includes the file {slug}.php from your current theme’s directory, a custom Include Tags other than header, sidebar, footer.

検索フォームテンプレート

<?php get_search_form(); ?>

get_search_form()タグは、現在のテーマディレクトリからsearchform.phpファイルをインクルードします。ファイルが見つからない場合は、検索フォームを生成します。

原文(翻訳元)This tag includes the file searchform.php from your current theme’s directory. If that file is not found, it will generate the search form.

詳細は、get_search_formとプラグインとテーマを2.7への移行を参照してください。

原文(翻訳元)See also get_search_form and Migrating Plugins and Themes to 2.7 for more detail.

コメントテンプレート

<?php comments_template(); ?>

comments_template()タグは、現在のテーマディレクトリからcomments.phpファイルをインクルードします。ファイルが見つからない場合は、代わりにwp-content/themes/default/comments.phpをインクルードします。メインインデックスやアーカイブページにコメントを表示するには、このタグを呼び出す前に”1″と$withcomments変数を設定する必要があります。

原文(翻訳元)This tag includes the file comments.php from your current theme’s directory. If that file is not found, it will instead include wp-content/themes/default/comments.php. To display comments on the main index or archive pages, you’ll need to set the $withcomments variable to “1” before calling this tag.

全てのテンプレートをインクルードする

WordPress はそれらの特定のテンプレートをインクルードするために、上記のタグを提供していますが、便利な方法は全てのファイルを含める事です。これを行うには、PHPの関数をインクルードを使用する必要があり、普通WordPressは便利に簡単に作れるように定義されています。: TEMPLATEPATH

原文(翻訳元)WordPress offers the above tags for including those specific Templates, but there is also a convenient way to include any file. To do so, you will need to use the include PHP function, and a constant WordPress conveniently defines for you to make things easy: TEMPLATEPATH.

header2.phpを呼び出すファイルをインクルードしたい時に呼びだします。あなたがファイルの情報を表示したいテンプレートに次の行を挿入します。

原文(翻訳元)Suppose you want to include a file called header2.php. Just insert the following line in your template where you want that file’s information to appear.

<?php include( TEMPLATEPATH . '/header2.php' ); ?>

例えば、get_header()とインクルードするであろう通常のheader.phpの代わりに異なるヘッダーをインクルードする手段としてこれを使用する事ができます。

原文(翻訳元)You could, for example, use this as a means of including a different header instead of the normal header.php which would be included with get_header().

NOTE:

  • TEMPLATEPATHは、現在のテンプレートディレクトリーの絶対パスを参照します(最後に/なしで)。インクルードファイルよりむしろURLの参照の詳細については、テンプレートからファイルを参照を見てください。
  • STYLESHEETPATHは、子テーマ内にあるファイルをインクルードするために使用すべきです。

原文(翻訳元)

  • TEMPLATEPATH is a reference to the absolute path to the current template directory (without the / at the end). For information on referencing URIs rather than including files, see Referencing Files From a Template.
  • STYLESHEETPATH should be used to include a file located within a child theme.

次は、”HTTP 404:見つかりません”エラーのテンプレートの非常にシンプルな例です(404.phpとしてテーマに含めることができます)。

原文(翻訳元)The following is a very simple example of a template for an “HTTP 404: Not Found” error (which you could include in your Theme as 404.php).

<?php get_header(); ?>
<?php get_template_part('nav'); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

パラメーター

get_header()、get_footer()、get_sidebar() は、1つのパラメーターを受け付けます。

原文(翻訳元)get_header(), get_footer() and get_sidebar() accepts one parameter:

$name

(string) (optional) sidebar-name.phpを呼び出します。例:sidebar-right.php,header-single.php、footer-8.php

原文(翻訳元)(string) (optional) Calls for sidebar-name.php. For Example: sidebar-right.php, header-single.php or footer-8.php.

Default: None

get_template_part()は、2つのパラメーターを受け付けます。

原文(翻訳元)get_template_part() accepts two parameters:

$slug

(string) (required){slug}.phpを呼び出します。例:nav.php

原文(翻訳元)(string) (required) Calls for {slug}.php. For Example: nav.php

Default: None

$name

(string) (optional) {slug}-{name}.phpを呼び出します。例:nav-home.php

原文(翻訳元)(string) (optional) Calls for {slug}-{name}.php. For Example: nav-home.php

Default: None

変更ログ

  • 1.5 :
    • get_header() was added to include the template header.
    • get_footer() was added to include the template footer.
    • get_sidebar() was added to include the template sidebar.
  • 2.5 :
    • The name parameter was added to get_sidebar().
  • 2.7 :
    • The name parameter was added to get_header() and get_footer().
    • get_search_form() was added to include the search form.
  • 3.0 :
    • get_template_part() was added to include the template other generic files.

ソースファイル

  • Browse source: wp-includes/general-template.php

関連ファンクションタグ

Include Tags: get_header, get_footer, get_sidebar, get_template_part, get_search_form, comments_template,

Sponsored Link