0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Wordpressにおけるテンプレート読み込みの関数(get_template_part)

Posted at

get_template_part関数について

同じコードを何度も書かないというプログラミングの常識があり、Wordpressでも
複数ファイルで、共通して使われるコードをテンプレート化して読み込む方法が、get_template_partとなります。

記述方法①(index.php内で記載)

   

()内は読み込みをするファイルのdirectoryとファイル名を記載します。
※ファイル名の拡張子(.php)の記載は不要です。

これでsample-file.phpの内容をindex.php内で読み込めるようになります。

記述方法②(index.php内で記載)

$の後にtemplateファイルのSlug名を記載することで、 sample.phpファイルを読み込むことができます。

記述方法③(index.php内で記載)

こちらの記載でsample-file.phpを読み込みできるようになります。

Slugについて(補足)

$Slugとは記事やカテゴリに付与される名前のことで、URLの一部になります。
日本語は使えず、英数字を使うことになります。 Slugを指定することで記事やページを指定できるようになります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?