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