Bandit University

 

WordPress Templates 101

WordPress templates are PHP files used to display the content that is stored in your database. Each WordPress theme is a collection of template files used to display posts and pages. How the data is displayed depends on how it is formatted in each template.

The only template file required in a theme is index.php. The style.css file while also required is not a template. Now that we have the basics out of the way, what template files does WordPress look for and when?

is_home()

Are we on the home page? Load home.php if it exists, else load index.php.

Order that templates are searched:

  • home.php
  • index.php

is_single()

Are we on a post entry? Load single.php if it exists, else load index.php.

Order that templates are searched:

  • single.php
  • index.php

is_page()

Are we on a page? Load assigned page template, else load page.php if it exists, else load index.php.

Order that templates are searched:

  • Assigned Page Template
  • page.php
  • index.php

is_category()

Are we on a category page? Load category-slug.php if it exists, else load category-id.php if it exists, else load category.php if it exists, else load archive.php if it exists, else load index.php.

Order that templates are searched:

  • category-slug.php (v2.9) - where slug is the actual slug of the category (ie: category-news.php)
  • category-id.php - where id is the actual id of the category (ie: category-4.php)
  • category.php
  • archive.php
  • index.php

is_tag()

Are we on a tag page? Load tag-slug.php if it exists, else load tag.php if it exists, else load archive.php if it exists, else load index.php.

Order that templates are searched:

  • tag-slug.php - where slug is the actual slug of the category (ie: tag-mytag.php)
  • tag.php
  • archive.php
  • index.php

is_author()

Are we on a author page? Load author.php if it exists, else load archive.php if it exists, else load index.php.

Order that templates are searched:

  • author.php
  • archive.php
  • index.php

is_search()

Are we on a search results page? Load search.php if it exists, else load index.php.

Order that templates are searched:

  • search.php
  • index.php

is_404()

Page not found? Load 404.php if it exists, else load index.php.

Order that templates are searched:

  • 404.php
  • index.php

You should now have a basic understanding of the template files used in WordPress and when they are loaded. If you would like additional information, please visit the Template Hierarchy page in the WordPress Codex.