• No results found

Template Hierarchy

In document Pro WordPress Theme Development (Page 30-37)

Now that the basic template files have been introduced, you can start to look at ways to customize the output of your theme with more specific template files. All the files discussed in this section work in a similar way to the index.php file and normally have some or all of the basic template files included inside them. If you take a look at the template hierarchy (see Figure 2-4), you see a plethora of options when creating custom templates for almost any part of your content. The diagram has actually become so big that to fit it in the book I included a partial section. (For a full view of the hierarchy, see http://wp-themes-book.com/02002.)

Take a look at some of the base files that appear on the right of the hierarchy: • page.php

• single.php

• attachment.php Figure 2-4. WordPress template hierarchy

Chapter 2 ■ theme anatomy and template hierarChy • archive.php • taxonomy.php • category.php • date.php • tag.php • author.php

Here are some of the special template files used for more specific purposes: • front-page.php

• home.php • search.php • 404.php

• comments-popup.php

The main thing to remember about the hierarchy is that the farther right the template appears, the more general it is. Any template file format to the left of the one on the same row is more specific to a certain page or type, and is given priority.

page.php

As its name suggests, this template is used to generate and display the pages in your WordPress site. If you need to create a custom template for a specific page, you can do it easily by using the page’s slug, which is usually a lowercase, hyphenated version of the page title. In WordPress, the page slug is located beneath the main title in the editor.

You can also use the page ID in place of the slug, but I don’t recommend it because it’s not very easy to see which template is for which page. I can't think of a situation in which you would often change the location of a page but keep the same format.

The two templates you need to remember when using pages are page.php for general use and page-{slug}.php for targeting specific pages for custom layout (where {slug} is the page slug of the targeted page).

single.php

The single.php template is used to display all individual posts. It is also used when custom post types have been created, and you want to display an individual post of that type. To target a specific post type, you need to use the name of that post type as you use the page slug to target a specific page. To create a template to display all standard posts in WordPress, use single-post.php, but remember that all individual posts fall back to single.php if single-post.php does not exist. My usual approach is to use single-posttype.php for all custom posts and single.php for the native post type in WordPress.

The single.php template is used to display all post attachments if you allow them to be linked to in WordPress. To create a more basic page for attachments, use the attachment.php template file.

attachment.php

The attachment.php file is used to display any attachments that can be directly linked to from the WordPress front end. When you upload images/videos/documents to WordPress, it creates the file’s path where it is stored in the WordPress file system (usually /wp-content/uploads), and then an attachment URL, that can be linked to directly to show the attachment within the front end of your WordPress site.

You can also create more specific template files for certain attachment types by using the MIME type as the template name:

• image.php • video.php • text.php

archive.php

The archive template is used whenever a group of posts or information is being accessed through the query. It can be used for anything from a date-specific archive, to posts by a certain author, or to posts that are in a certain category. The main archive.php template displays everything, but as shown in the template hierarchy shown in Figure 2-6, there are many template files that can be used to overwrite the archive.php.

Chapter 2 ■ theme anatomy and template hierarChy

The archive types include the following: • category • tag • author • date • taxonomy

Note

Custom post–type specific archives are covered in more detail in Chapter 4.

You can also target specific categories, tags, and authors by using the specific category, tag, or author name (author nicename as used in WordPress). The date template file catches any archives relating to a specific date period including year, month, and day; but you cannot target other types of date information.

The taxonomy template can be used to target custom taxonomies you create in WordPress as well as specific terms in that template. To target a custom taxonomy, append the taxonomy name to the template name (for example, taxonomy-taxonomyname.php). To access a specific term of a specific taxonomy, append the term name to it (for example, taxonomy-taxonomyname-termname.php).

Now that I’ve covered some of the more general templates available in the template hierarchy, look at some of the templates that have more specific functionality in WordPress.

front-page.php

The front-page template is used when a static front page has been selected from the WordPress admin options in Settings ➤ Reading (see Figure 2-7). When this option is set, the page template is overwritten with front-page.php ahead of any others, enabling you to create a custom home page for your site based on a specific template file and making that template file a lot easier to spot among your template files.

home.php

The home template is used in two circumstances: as the main template for the home page for your web site or as the main listing page template of your site when you have set up a static front page using the reading settings, as shown in Figure 2-7.

search.php

The search page template displays the results of any search performed on the web site. To get this template to load a search, you need to submit a GET request to the home page of the site. For example, mysite.com?s=about would search mysite.com for the term “about”, and the search template would be used to display those results.

In this template, to display the search that was performed, you can use the the_search_query()function: Figure 2-7. Set up a static page at the top of the reading settings

Chapter 2 ■ theme anatomy and template hierarChy

It is also possible, although it requires a bit of trickery, to change the default location of the search while retaining the correct page template, thanks to this code snippet from Paul Davis:

function search_url_rewrite(){

if(is_search() && !empty($_GET['s'])){

wp_redirect(home_url("/search/"). urlencode(get_query_var('s'))); exit();

} }

add_action('template_redirect', 'search_url_rewrite');

This function adds on to the 'template_redirect' hook, which determines the page template WordPress needs to load. When a search is detected, it redirects the URL to /search/searchterm, which creates better URL structures and means you are no longer required to use a GET to perform a search.

404.php

The 404 template is displayed by WordPress when it cannot find a result for the query for a specific page or post. General queries for an archive page that turn up no results still load the correct archive template with errors for that handled in the template file using the Loop (more on that in the next chapter).

A custom 404 page helps users who may have been redirected to the wrong page or have mistyped a URL by giving them a way back into the site and allowing them to find what they were looking for. Have a bit of fun, as with the GitHub 404 page in Figure 2-8. A fun 404 page is good design because it can help reduce any user frustration from not finding something on your site.

In document Pro WordPress Theme Development (Page 30-37)