WordPress 模板包含標籤指的是在一個模板文件中包含另一個模板文件時涉及到的其它標籤,在 php 中使用包含文件的函數是

  • include
  • include_once
  • require
  • require_once

而在 WordPress 模板中包含另外一個模板使用了以下函數:

  • get_header()
  • get_footer()
  • get_sidebar()
  • get_template_part()
  • get_search_form()
  • comments_template()

1 、 get_header():包含頭部模板
用法:

<?php get_header(); ?>

説明:
get_header() 標籤包含了當前使用主題的頭部模板文件: header.php 或者 header-{name}.php ,如果沒有該文件則使用系統默認的頭部模板文件: wp-includes/theme-compat/header.php 。

2 、 get_footer():包含底部模板
用法:

<?php get_footer(); ?>

説明:
get_footer() 標籤包含了當前使用主題的底部模板文件:footer.php 或者 footer-{name}.php , 如果沒有該文件則使用系統默認的底部模板文件: wp-includes/theme-compat/footer.php.
3 、 get_sidebar():包含側邊欄模板
用法:

<?php get_sidebar(); ?>

説明:
get_sidebar() 標籤包含了當前使用主題的側邊欄模板文件:sidebar.php 或者 sidebar-{name}.php, 如果沒有該文件則使用系統默認的側邊欄模板文件:wp-includes/theme-compat/sidebar.php 。
4 、 get_template_part():包含自定義模板
用法:

<?php get_template_part(); ?>

説明:
get_template_part() 標籤包含了當前使用主題的自定義的模板文件: {slug}.php 或者 {slug}-{name}.php 。

5 、 get_search_form():包含搜索框模板
用法:

<?php get_search_form(); ?>

説明:
get_search_form() 標籤包含了當前使用主題的搜索框的模板文件: searchform.php ,如果沒有該文件則使用系統生存默認的搜索框。

6 、 comments_template():包含評論模板
用法:

<?php comments_template(); ?>

comments_template() 標籤包含了當前使用主題的評論的模板文件 comments.php, 如果沒有該文件則使用系統默認的評論模板文件: wp-includes/theme-compat/comments.php 。

標籤應用實例

以下代碼是一個簡單的 404 頁面模板實例,模板頁面顯示 「HTTP 404: Not Found」,主題中你可以將該文件命名為 404.php 。

<?php get_header(); ?>
<?php get_template_part('nav'); ?>
<h2>Error 404 - Not Found</h2>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

模板參數

get_header(), get_footer() 和 get_sidebar() 接收以下的一個參數:

$name
(string) (可選) 調用 sidebar-{name}.php 格式的模板文件。 例如: sidebar-right.php, header-single.php 或者 footer-8.php.
默認: None

get_template_part() 接收以下兩個參數:

$slug
(string) (必須) 調用 {slug}.php 格式的模板文件。例如: nav.php
默認: None
$name
(string) (可選) 調用 {slug}-{name}.php 格式模板文件。例如: nav-home.php
Default: None