Template Receive Updates For This Category
June 2, 2011 WordPress Lessons 0 0
Introduction
WordPress Templates fit together like the pieces of a puzzle to generate the web pages on your WordPress site. Some templates (the header and footer template files for example) are used on all the web pages, while others are used only under specific conditions.
What this article is about
This article seeks to answer the following question:
Which template file(s) will WordPress use when it displays a certain type of page?
Who might find this useful
Since the introduction of Themes in WordPress 1.5, Templates have become more and more configurable. In order to develop WordPress themes, a proper understanding of the way WordPress selects template files to display the various pages on your blog is essential. If you seek to customize an existing WordPress theme, this article aims to help you decide which template file needs editing.
Using Conditional Tags
Using Conditional Tags
WordPress provides more than one way to match templates to query types. WordPress Theme developers can also use Conditional Tags to control which templates will be used to generate a certain page. Some WordPress Themes may not implement all of the template files described here. Some Themes use conditional tags to load other template files. See the Conditional Tags page and 8220;Query Based8221; in Theme Development for more information.
The Template File Hierarchy
The General Idea
First, WordPress matches every Query String to query types 8211; i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.
Templates are then chosen 8211; and web page content is generated 8211; in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.
WordPress looks for 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.
With the exception of the basic index.php template file, Theme developers can choose whether they want to 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 (the Theme s home page template file) will be used.
Examples
If your blog is at http://example.com/wp/ and a visitor clicks on a link to a category page like http://example.com/wp/category/your-cat/, WordPress looks for a template file in the current Themes directory that matches the categorys ID. If the categorys ID is 4, WordPress looks for a template file named category-4.php. If it is missing, WordPress next looks for a generic category template file, category.php. If this file does not exist either, WordPress looks for a generic archive template, archive.php. If it is missing as well, WordPress falls back on the main Theme template file, index.php.
If a visitor goes to your home page at http://example.com/wp/, WordPress first determines whether it has a static front page. If a static front page has been set, then WordPress loads that page according to the page template hierarchy. If a static front page has not been set, then WordPress looks for a template file called home.php and uses it to generate the requested page. If home.php is missing, WordPress looks for a file called index.php in the active themes directory, and uses that template to generate the page.
Visual Overview
The Template Hierarchy In Detail
The following sections describe the order in which template files are being called by WordPress for each query type.
Home Page display
- home.php
- index.php
Front Page display
- front-page.php – Used for both Your latest posts or A static page as set in the Front page displays section of Settings -> Reading
- Page display rules – When Front page is set in the Front page displays section of Settings -> Reading
- Home Page display rules – When Posts page is set in the Front page displays section of Settings -> Reading
Single Post display
- single-{post_type}.php 8211; If the post type were product, WordPress would look for single-product.php.
- single.php
- index.php
Page display
custom template 8211; Where custom template is the Page Template assigned to the Page.
page-{slug}.php 8211; If the page slug is recent-news, WordPress will look to use page-recent-news.php
page-{id}.php 8211; If the page ID is 6, WordPress will look to use page-6.php
page.php
index.php
Category display
category-{slug}.php 8211; If the categorys slug were news, WordPress would look for category-news.php
category-{id}.php 8211; If the categorys ID were 6, WordPress would look for category-6.php
category.php
archive.php
index.php
Tag display
tag-{slug}.php 8211; If the tags slug were sometag, WordPress would look for tag-sometag.php
tag-{id}.php 8211; If the tags ID were 6, WordPress would look for tag-6.php
tag.php
archive.php
index.php
June 2, 2011 WordPress Lessons 0 0
This document provides a brief examination of the animal known as the WordPress template tag, to help those who may be new to WordPress and PHP understand what template tags are and how they are used.
A WordPress template tag is made up of three components:
- A PHP code tag
- A WordPress function
- Optional parameters
These are explained below.
PHP code tag
WordPress is built with the PHP scripting language. Though you certainly do not need to be a PHP developer to use it, knowing a little about the language can go a long way in getting the most out of WordPress. Here we provide a tiny bit of that PHP knowledge:
The above shows the opening () tag elements used to embed PHP functions and code in a HTML document, i.e. web page. There are a number of ways to embed PHP within a page, but this is the most 8220;portable,8221; in that it works on nearly every web server-as long as the server supports PHP (typically a documents filename also needs to end with the extension .php, so the server recognizes it as a PHP document).
Anything within this tag is parsed and handled by the PHP interpreter, which runs on the web server (the interpreter is the PHP engine that figures out what the various functions and code do, and returns their output). For our purposes, the PHP tag lets you place WordPress functions in your page template, and through these generate the dynamic portions of your blog.
WordPress function
A WordPress or template function is a PHP function that performs an action or displays information specific to your blog. And like a PHP function, a WordPress function is defined by a line of text (of one or more words, no spaces), open and close brackets (parentheses), and typically a semi-colon, used to end a code statement in PHP. An example of a WordPress function is:
the_ID();
the_ID() displays the ID number for a blog entry or post. To use it in a page template, you slip it into the PHP tag shown above:
This is now officially a WordPress template tag, as it uses the PHP tag with a WordPress function.
Optional parameters
The final item making up a template tag is one you wont necessarily make use of unless you want to customize the tags functionality. This, or rather these, are the parameters or arguments for a function. Here is the template function bloginfo(), with the show parameter being passed the name value:
If your blogs name is Super Weblog, the bloginfo() template tag, when using name as the show parameter value, will display that name where its embedded in your page template.
Not all template tags accept parameters (the_ID() is one), and those which do accept different ones based on their intended use, so that the_content() accepts parameters separate from those which get_calendar() can be passed.
