HCDコンサルティング(旧・中川勉社会保険労務士事務所FPウェブシュフ)のブログ

get_template_part( ‘content’, get_post_format() );はcontent.phpをincludeする

Twenty Twelveのループ内では、get_template_partとget_post_formatを組み合わせた、get_template_part( ‘content’, get_post_format() );がcontent.phpを呼び出すようになっています。最初はよくわからなかったのでそのメカニズムをメモ。

get_template_part

これは、別のphpファイルをincludeするためのテンプレートタグです。

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

という形式で、{slug}-{name}.phpを呼び出してくれます。

{slug}-{name}.phpがない場合にはcodexにも書かれているように、{slug}.phpを呼び出してくれます。

さらにそれもなければ、親テーマの{slug}-{name}.php、{slug}.phpを呼び出しに行ってくれます。

get_post_format

get_post_formatは投稿のformatを文字列で返してくれます。

formatというのは、投稿の編集画面の右下あたりの以下の部分で設定するのですが、通常は「標準」ですよね。

ところで、formatが「標準」の場合、get_post_formatは「standard」を返しそうですが、そうではないんです。

Function Reference/get post format ≪ WordPress Codexによれば「false」という文字列を返します。

ここを抑えずにtwenty twelveのindex.pxp、category.php、single.php等を眺めていると、いったいどのファイルをincludeするのかわけがわからなくなります。

get_template_part( ‘content’, get_post_format() );がincludeするファイル

結局、formatが「標準」に設定されている投稿の場合、get_post_format()==falseなので

<?php get_template_part( 'content', get_post_format() ); ?>

<?php get_template_part( 'content', 'false' ); ?>

となります。

twenty twelveでは、content-false.phpというファイルはありませんから、子テーマを作ってその中にcontent-false.phpをこしらえていない限り、

<?php get_template_part( 'content', get_post_format() ); ?>

によってincludeされるファイルは、content.phpになります。

子テーマを利用して子テーマファイル内にcontent.phpがないときは、親テーマのcontent.phpがincludeされます。


投稿日

カテゴリー:

投稿者:

 最終更新日:

タグ: