> WordPress中文手册 > wordpress模板包含标签使用介绍

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