Quick Introduction – I’m Sarah and I will be writing each Sunday on intermediate to advanced WordPress topics including template development, tips and tricks, PHP coding for use in WordPress and plugin development. I’ll be basing my posts on the latest version of WordPress, and also assume an understanding of HTML and CSS, and presume you don’t run at the sight of a little PHP
I’d love to hear what you want to read about on this subject so please let me know in the comments.
At the very least a WordPress theme requires just two files, style.css and index.php. Everything on your site can run off just these two files. However, having just two files would create the same look throughout the site on every page. Whilst you could use some conditional tags in your index file to control what is displayed on different sections of the site, this would create a larger and more complicated file to deal with, especially if you decide to change things in, say, 6 months time. So to keep things easier to read you can work with several files to keep things organised, allowing you to control different aspects of each section easily.
WordPress runs off a Template Hierarchy, which means it will look for a particular file for the current page viewed, in the active theme directory and if it cannot find that file it will then look for the next filename in the hierarchy and so on until it finds a match. Understanding this hierarchy can help with creating your own template and also modifying one of the many free templates available online.
So we have the several sections of a WordPress site including Front Page, Categories, Archives, Single Post and Static Pages. The Front Page is best kept as index.php (home.php could be used however this is no longer necessary since WordPress 2.1). The hierarchy of the other sections are:
This means that if you view a specific category e.g. ‘Design’ with an ID of 3, WordPress will first look in your template directory for the category-3.php file, if it doesn’t find that it then looks for category.php, if that doesn’t exist it will look for archive.php and finally it will use index.php as a template for laying out the page.
If you intend to have two or more of these additional section files eg. index.php and archive.php, then it’s more efficient to separate your general header and footer into their own files, and the sidebar/menu into its own file if it cannot fit into the header or footer, or if it is not going to be on every page (like the way the default theme Kubrick works).
You may wonder why the need for the various files (or maybe not!). This hierarchy gives the option to have a different structure for different sections, for example excerpts of posts displayed on the front page, category and archive pages, the full post on the single page with a comments form, and the full content on a static page, without the comments form and category details (as pages are not put in categories). Alternatively you may want a different structure for your category and archive pages, compared to your front page.
Hopefully this has explained the template hierarchy, giving a basic understanding of how the file system works. Understanding the basic concept of this means we can start to look at developing your own theme from scratch in the forthcoming posts.
Author comments are in a darker gray color for you to easily identify the posts author in the comments
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
Very good post Sarah. The wordpress codex is full of a lot of great info but it can be a nightmare trying to decipher the damn thing!!
Cheers Kevin. I think with the codex it’s like a full working manual, and most people don’t need to know every single piece of info on there, which can make it seem a bit overwhelming at times.
Same article for russian people made by Sonica
If you are interested in having multiple categories use the same category template, I have nearly completed development on a plugin which will make this possible (as well as allow individual posts to leverage separate single.php templates based on the category the post is assigned to). I’m actually looking for some beta testers to make sure it doesn’t cause Wordpress chaos before releasing the magical version 1.0 to the world.