LoginSignup
40
37

More than 5 years have passed since last update.

Wordpressでテンプレートファイルをロードする5つの方法とその違い

Last updated at Posted at 2013-10-10

Wordpressはテンプレートファイルを読み込む関数が複数あって、どのような違いがあるかわからなかったので調べてみた。

1. include

まずはPHP自体の機能として備わっている外部ファイルの読み込みを行う include を使う方法。

include(TEMPLATEPATH . '/template-name.php');

長所:最も高速
短所:ファイルの存在の有無をチェックしない

2. load_template()

次は load_template()、ここからはWordpressの関数

load_template(TEMPLATEPATH . '/template-name.php');

長所:高速
短所:やっぱりファイルの有無のチェックはしない。フルパスで記述しないといけない。

3. locate_template()

続いては locate_template() を使う方法

locate_template($template_names, $load);

長所:
ファイルの探索をしてくれるので絶対パスじゃなくて良い.
ファイルを include することもできる。 $load が true ならよみこんで false ならパスを追加するだけ。

$template_names はテンプレート名が入った配列。既にここに存在していたらincludeはされないようにしてくれる。

4. get_query_template()

書くのめんどくさくなってきた。

…次は get_query_template()を使う方法!

include(get_query_template('template-name'));

拡張子なしで読み込んでくれるよ、やったー!

5 get_template_part()

Wordpress3.0から使える関数。とてもパワフル、というか多分これしか基本的に使わない。なんだったんだ、今までの説明は…。

get_template_part('loop'); // general loop, file 'loop.php'
get_template_part('loop', 'index'); // loop for index, file 'loop-index.php'
get_template_part('loop', 'single'); // loop for single post, file 'loop-single.php'

な感じで読み込めます。

参考

5 Ways To Include Template Files In WordPress - Deluxe Blog Tips
http://www.deluxeblogtips.com/2010/06/wordpress-include-template-files.html

40
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
40
37