get_template_part

Sponsored Link

テンプレート部分を(ヘッダ、サイドバー、フッタ以外の)テンプレートを読み込んでください。親テーマのセクションを置き換えるために、子テーマのコードと簡単な方法でセクションを再利用するためのテーマを作りやすくします。

テーマの為に名付けられたテンプレート部分をインクルードします。あるいは、名前は、その時専門の部分をインクルードすることになるであろう特殊な部分を指定します。テーマに{slug}.phpファイルが含まれていない場合は、テンプートはインクルードしないでしょう。

パラメーターのためのファイルは、”{slug}-{name}.php”を呼び出します。

Sponsored Link

Load a template part into a template (other than header, sidebar, footer). Makes it easy for a theme to reuse sections of code and an easy way for child themes to replace sections of their parent theme.

Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

For the parameter, if the file is called “{slug}-{name}.php”.

get_template_partのテンプレートタグ使用方法

<?php get_template_part( $slug, $name ) ?> 

get_template_partのパラメーター

$slug

スラッグ名は、汎用のテンプレートです。

(string) (required) The slug name for the generic template.

デフォルト:なし

Default: None

$name

(string) (optional) 専門のテンプレートの名前です

(string) (optional) The name of the specialised template.

デフォルト:なし

Default: None

子テーマにloop.phpを使用します。

テーマフォルダは、wp-content/themesを想定しています。親テーマはtwentytenで、そして、子テーマはtwentytenの子です。その時次のコードになります。

Assuming the theme folder is wp-content/themes, that the parent theme is twentyten, and the child theme is twentytenchild, then the following code —

<?php get_template_part( 'loop', 'index' ); ?>

この順序で指定されたファイルのPHPrequire()を行います。

will do a PHP require() for the named files in this order:

  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentytenchild/loop.php
  3. wp-content/themes/twentyten/loop-index.php
  4. wp-content/themes/twentyten/loop.php

ナビゲーション

一般的なnav.phpテンプレートファイルを使用するテーマのナビゲーションバーを追加します。

Adding a navigation bar to theme using a gereric nav.php template file:

<?php get_template_part( 'nav' );           // Navigation bar (nav.php) ?>
<?php get_template_part( 'nav', '2' );      // Navigation bar #2 (nav-2.php) ?>
<?php get_template_part( 'nav', 'single' ); // Navigation bar to use in single pages (nav-single.php) ?>

ソース:wp-tricks.co.il – 一般的なナビゲーションバーを作成するためにget_template_part()を使用します。

source: wp-tricks.co.il – using get_template_part() to create generic navigation bar

備考

  • *用途:locate_template()
  • *用途:do_action() は、’get_template_part{$slug}’ アクションを呼び出します。

* Uses: locate_template()
* Uses: do_action() Calls ‘get_template_part{$slug}’ action.

ソースファイル

get_template_part()は、wp-includes/general-template.phpに位置しています。

get_template_part() is located in wp-includes/general-template.php.

変更ログ

* Since: 3.0.0

関連テンプレートタグ

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

Sponsored Link