wordpress: modifying themes
The Template Hierarchy
The trick to modifying your theme is in knowing which files to edit. And to know that, you need to learn a bit about the WordPress template hierarchy.
When a request for a page on your site is received, WordPress first examines the URL and determines what type of page it is. For the sake of example, let's say a user has requested a specific category page on your site:
http://example.com/categories/books. From the URL, WordPress knows that it needs to display a category page. It also knows, internally, that a category is a type of an archive. It also knows that this isn't just any category page, it's a specific category page (the one that has "books" as a slug).
To determine which template file to use to display this particular category view, WordPress will step through a hierarchy of options, starting with the most specific template and falling back to the most general. WordPress will follow this chain of logic:
1) Because you might want to use a special template just to show the Books category, that's the most specific case, so WordPress will first look for a template called
category-books.php. When creating templates for specific categories like this, you can also name template files
category-5.php, where "5" is the ID of the category you want to customize. In other words, you can use either the category slug or its database ID when naming this template.
2) If that template file doesn't exist, WordPress will try to find the general-purpose category template, which is always named
category.php (this would be used for all category views).
3) If the current theme doesn't have a
category.php template, WordPress will think "Well, a category is a specialized form of archive," so it will look for a template file called
archive.php (in this case, the
archive.php template would be used for both category views and date-based views such as "All posts from March").
4) If there is no
archive.php template, WordPress will fall back to using the most generic possible template, which is
In other words, WordPress will traverse a logical hierarchy of options when deciding which template to use, starting with the most specific option and falling back to more generic options until it reaches
index.php. This is why it's possible (though not recommended) to have a theme that consists of nothing but a stylesheet and an
Other template-choosing decisions in WordPress are made in a similar way. For more examples, see the flow-chart below, which links to the Codex documentation on the Template Hierarchy:
So, if you want to modify the way search results are displayed on your site, you would look at the Template Hierarchy and determine that that view is handled by a template called
search.php. Then you would look in your theme for a file of that name. If it exists, edit it! If it doesn't, you'll see that you need to edit
index.php instead. And so on.
Once you open a file for editing, you'll be faced with a combination of standard HTML, basic PHP-language programming structures such as loops and tests, and WordPress function calls written in PHP. See the examples in this tutorial to see how it all fits together.