WordPressのテンプレート内で外部のPHPファイルを読み込む方法です。
header.phpを読み込む
<?php get_header(); ?>
footer.phpを読み込む
<?php get_footer(); ?>
include でファイルを読み込む
<?php
include 'パス/ファイル名';
//または
include ('パス/ファイル名');
?>
includeは制御構造なので「()」は有りなしどちらでも動作します。
get_template_part() でファイルを読み込む
<?php
/*
list.php
list-news.php
を読み込む
*/
get_template_part('list');
get_template_part('list', 'news');
/*
parts/list.php
parts/list-news.php
を読み込む
*/
get_template_part('parts/list');
get_template_part('parts/list', 'news');
?>
参考リンク
- 【PHP超入門】式・文・構文・言語構造・制御構造について
- phpのincludeの記述について教えて下さい。
- 【WordPress】任意のテンプレートパーツを読み込む関数[get_template_part() ;]の使い方。