WordPress Themes .. Pages and Template Hierarchy

Pages: 1 2 3

Theme Template Files and the Template Hierarchy

We have seen that WordPress populates the content of the_post() object based upon the URL fed to the server.  Often we want to customize the information as displayed to the browser based upon the post-type.  For example, for content of type “post” we may want to display the publish date, the author, etc.  For PAGES, we may, for example, want to apply different styling.

We could use Conditional Template Tags, such as is_page() and is_single() and use PHP in the index.php file to control what is rendered.  There is, however, an easier way.  WordPress recognizes Template Files with specific names which will then automatically be selected to render output of a particular type.  These Template Files are optional components of a theme.

Introduction to Template Files

We will quote / paraphrase from the WordPress CODEX where the Template Hierarchy is discussed.

WordPress uses the information contained within each link on your web site — to decide which template or set of templates will be used to display the page.

First, WordPress decides what type of page (a search page, a category page, the home page etc.) is being requested.

WordPress selects template files with specific names in the current Theme’s directory and uses the first matching template file listed under the appropriate query section below.

A theme can implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, index.php will be used.

The diagram below depicts the WordPress process in general.

WordPress Server Processing


Let us illustrate some of this in the simple03 theme.

Pages: 1 2 3

Leave a Reply

You must be logged in to post a comment.