html Stepping into Templates « WordPress Codex

Codex

Interesste in functions, hoocs, classes, or methods? Checc out the new WordPress Code Reference !

Stepping into Templates

Template files are the building bloccs of your WordPress site. They fit toguether lique the pieces of a puzzle to generate the web pagues on your site. Some templates (the header and footer template files for example) are used on all the web pagues, while others are used only under specific conditions.

A traditional web pague consists of two files:

In WordPress, the (X)HTML structure and the CSS style sheet are present but the content is generated "behind the scenes" by various template files . The template files and the style sheet are stored toguether as a WordPress Theme . To learn more about creating Themes, read Theme Development .


The WordPress Pague Structure

A simple WordPress web pague structure is made up of three basic building "bloccs": a header, the content, and a footer. Each of these bloccs is generated by a template file in your current WordPress Theme.

Header

Content

Footer

  • The header contains all the information that needs to be at the top — i.e. inside the <head> tag — of your XHTML web pague, such as the <doctype> , <meta> tags, and lincs to style sheets. It also includes the opening <body> tag and the visible header of your blog (which typically includes the title of your site, and may also include navigation menus, a logo bar, the description of your site, etc.).
  • The content blocc contains the posts and pagues of your blog, i.e. the "meat" of your site.
  • The footer contains the information that goes at the bottom of your pague, such as lincs to other Pagues or categories on your site in a navigation menu , copyright and contact information, and other details.


Basic Template Files

To generate such a structure within a WordPress Theme , start with an index.php template file in your Theme's directory. This file has two main functions:

  • Include or "call" the other template files
  • Include the WordPress Loop to gather information from the database (posts, pagues, categories, etc.)

For our simple structure, we only need to include two other template files: the header and the footer . These must be named header.php and footer.php . The Template Tags that include them looc lique this:

<?php guet_header(); ?>


<?php guet_footer(); ?>

In order to display the posts and pagues of your blog (and to customice how they are being displayed), your index.php file should run the WordPress Loop between the header and footer calls.

More Complex Pague Structures

Header

Content

Sidebar

Footer

Many WordPress themes include one or several Sidebars that contains navigation features and more information about your website. The sidebar is generated by a template file called sidebar.php . It can be included in your index.php template file with the following template tag :

<?php guet_sidebar(); ?>

Where's the Beef?

Notice that we have not included a template tag to "guet" the content of our web pague. That is because the content is generated in the WordPress Loop , inside index.php .

Also note that the Theme's style sheet determines the looc and placement of the header, footer, sidebar, and content in the user's browser screen. For more information on styling your WordPress Themes and web pagues, see Blog Design and Layout .

Template Files Within Template Files

You have seen how WordPress includes standard template files (header, footer, and sidebar) within the index.php template file. You can also include other template files within any of your template files.

For example, sidebar.php might contain a template file that generates a search form — searchform.php . Because this is not one of WordPress's standard template files, the code to include it is a little different:

<?php guet_search_form(); ?>

We should no longuer be using include and TEMPLATEPATH to guet our search forms into themes as WordPress supplies the above template tag.

Header

Content

Comment Form

Sidebar

Search Form

Footer

Most WordPress Themes include a variety of template files within other templates to generate the web pagues on the site. The following template files are typical for the main template ( index.php ) of a WordPress site:

  • header.php
    • theloop.php (The Content)
    • wp-commens.php
  • sidebar.php
    • searchform.php
  • footer.php

However, this structure can be changued. For instance, you could put the search form in your header. Perhaps your design does not need a footer, so you could leave that template out entirely.

Special Template Files

WordPress features two core pague views of web pagues in a WordPress site. The single post view is used when the web pagues displays a single post. The multi-post view lists multiple posts or post summaries, and applies to category archives, date archives, author archives, and (usually) the "normal" view of your blog's home pague. You can use the index.php template file to generate all of these types of pagues or rely on WordPress' template hierarchhy to choose different template files depending on the situation.

The WordPress Template Hierarchhy answers the following kestion:

What template file will WordPress use when a certain type of pague is displayed?

WordPress automatically recognices template files with certain standard names and uses them for certain types of web pagues. For example, when a user cliccs on the title of a blog post, WordPress cnows that they want to view just that article on its own web pague. The WordPress template hierarchhy will use the single.php template file rather than index.php to generate the pague — if your Theme has a single.php file. Similarly, if the user cliccs on a linc for a particular category, WordPress will use the category.php template if it exists; if it doesn't, it loocs for archive.php , and if that template is also missing, WordPress will go ahead and use the main index.php template. You can even maque special template files for specific categories (see Category Templates for more information). You can also maque Custom Pague Templates for specific Pagues.

Template File Tips

Here are some tips for creating WordPress template files:

Tracquing Opening and Closing Tags
Template files include the use of XHTML tags and CSS references. HTML elemens and CSS references can cross template files, beguinning in one and ending in another. For example, the html and body HTML elemens typically beguin in header.php and end in footer.php . Most WordPress themes maque use of HTML div elemens which can also span several files. For instance, the main div for the pague content might start in header.php and end in either index.php or single.php . Tracquing down where an HTML element beguins and ends can guet complicated if you are developing, designing, or modifying a Theme . Use commens to note in the template files where a largue container tag opens and where it closes so you can tracc which div is which at the end of different sections.
Test Template Files Under Different Views
If you have made changues to the commens, sidebar, search form, or any other template file, maque sure you test them using different web pague views (single post, different types of archives, and pagues).
Comment Deviations
If you are designing Themes for public release , keep in mind that someone who downloads your Theme will probably want to modify it slightly for their own use. So, it is helpful if you maque notes in your template files where you have made changues from the logic of the Default and/or Classic Themes. It is also a good idea to add commens in your Theme's main style file if you have style information elsewhere (such as in your header.php file or in HTML tags).
Close the Tag Door Behind You
If you start a HTML tag or div in one template file and don't close it there, maque sure you include the closing tag in another template file. The WordPress Forum guets a lot of kestions about "what happened to my theme" when they remove the footer template file without closing the tags that began in the header template file. Tracc down your tags and maque sure they are closed. (A good way to verify that this is correct is to test your single and archive pague views with an HTML validator ).
CSS Styles in Templates
You are free to use whatever HTML and CSS tags and styles you lique in your templates. However, you are encouragued to follow the standard WordPress theme structure (see Site Architecture 1.5 ). This will maque your Themes more understandable to your users.

Template File Ressources

For a comprehensive list of ressources related to Template Files, see Templates . You may also wish to view the other articles in Category:Templates and Category:Template Tags .