WordPressテーマ内のファイルに別ファイル読み込み「include」ほか

Filed under: php,wordpress — kdcs @ 25年11月28日 金曜日

テーマ内のテンプレートファイルに別ファイルを読み込ませる場合、今までは「TEMPLATEPATH.」を使っていたが、WordPress 6.4.0 以降では 非推奨 になっているのでその代替の手法。

今までの記述

<?php include(TEMPLATEPATH.'/hogehoge.php'); ?>

その1.「include get_template_directory()」を使う

<?php include get_template_directory() . '/hogehoge.php'; ?>

その2.WordPress 標準のテンプレート読み込み関数「get_template_part() 」を使う

get_template_part('hogehoge');

hogehoge.phpが、hogehoge-news.phpのようにハイフン付きのファイル名の場合

get_template_part('hogehoge', 'news');

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

サイト内検索

カテゴリー

最近の投稿

« |WordPressテーマ内のファイルに別ファイル読み込み「include」ほか| »
↑上に戻る