Codex

Include Tags

The Template include tags are used within one Template file (for example index.php) to execute the HTML and PHP found in another template file (for example header.php). PHP has a built in include() statement for this purpose, but these WordPress template tags make including certain specific files much easier.

See Using Themes and Theme Development for more information about Templates and Themes.

Contents

The Header Template

<?php get_header(); ?>

This tag includes the file header.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/header.php.

The Footer Template

<?php get_footer(); ?>

This tag includes the file footer.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/footer.php.

The Sidebar Template

<?php get_sidebar(); ?>

This tag includes the file sidebar.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/sidebar.php.

<?php get_sidebar('right'); ?>

Causes the template TEMPLATEPATH . 'sidebar-right.php' to be included. Note: the ability to specify a particular sidebar, meaning more than one sidebar template can be used in a theme, was added with Version 2.5.

The Search From Template

<?php get_search_form(); ?>

This tag includes the file searchform.php from your current theme's directory. If that file is not found, it will generate the search form.

See also get_search_form and Migrating Plugins and Themes to 2.7 for more detail.

The Comments Template

<?php comments_template(); ?>

This tag includes the file comments.php from your current theme's directory. If that file is not found, it will instead include wp-content/themes/default/comments.php. To display comments on the main index or archive pages, you'll need to set the $withcomments variable to "1" before calling this tag.

Including Any Template

WordPress offers the above tags for including those specific Templates, but there is also a convenient way to include any file. To do so, you will need to use the include PHP function, and a constant WordPress conveniently defines for you to make things easy: TEMPLATEPATH.

Suppose you want to include a file called header2.php. Just insert the following line in your template where you want that file's information to appear.

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

You could, for example, use this as a means of including a different header instead of the normal header.php which would be included with get_header().

NOTE:

  • TEMPLATEPATH is a reference to the absolute path to the current template directory (without the / at the end). For information on referencing URIs rather than including files, see Referencing Files From a Template.
  • STYLESHEETPATH should be used to include a file located within a child theme.

Example

The following is a very simple example of a template for an "HTTP 404: Not Found" error (which you could include in your Theme as 404.php).

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

Parameters

get_header(), get_footer() and get_sidebar() accepts one parameter:

$name
(string) (optional) Calls for sidebar-name.php. For Example: sidebar-right.php, header-single.php or footer-8.php.
Default: None

Changelog

Source File

Related

get_header, get_sidebar, get_search_form, comments_template, get_footer

See also index of Function Reference and index of Template Tags.