Web production note

 【更新日 :

【WordPress】テンプレート内で外部のPHPファイルを読み込む

Category:
WordPress

WordPressのテンプレート内で外部のPHPファイルを読み込む方法です。

header.phpを読み込む

<?php get_header(); ?>

footer.phpを読み込む

<?php get_footer(); ?>

include でファイルを読み込む

includeは制御構造なので「()」は有りなしどちらでも動作します。

<?php 
	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'); 
?>

参考リンク