WordPress is an incredibly powerful content management system powering countless websites, big and small – and for good reason. It enables users to set up, tweak and customize a website without having to touch a single line of code, and it helps developers showcase their talents, skills, and creativity. While all developers have their own favorite tricks and unique coding styles, there are certain things every developer should pay attention to. In WordPress, a page is essentially a combination of different template files. Today, we’ll be taking a closer look at WordPress template hierarchy and introducing you to some of the most important rules and best practices.
WordPress allows you to include as many templates and theme files as you want, which is why it’s so important you understand how WordPress template hierarchy works. However, before we jump into actually explaining what WordPress template hierarchy is and how it works, let’s take a look at what templates you need to begin with.
Traditional websites are made of an HTML and CSS file. In WordPress, however, both the HTML structure and the CSS style sheet are present, but the content is created by various template files. So, in order to set up a WordPress theme, you’ll need to have at least index.php and style.css.
Other Common WordPress Templates
As we mentioned before, there’s no specific limit when it comes to the number of WordPress templates, but there are a few that are commonly found in many themes. For example, if you want to set up a homepage that’s different from index.php, you should add home.php. WordPress will first try to load home.php, and only if it’s missing or corrupt, it will use index.php. You can also use single.php, a template often used for blog posts that visually differ from the rest of the theme. Archive.php controls the presentation of your archived data, such as categories, tags, dates and authors. You can also customize each of these categories by creating a category-id.php.
How Does WordPress Template Hierarchy Work?
Templates determine what every page of the website looks like. For example, when a visitor requests to access a part of your website, WordPress will interpret the URL and determine which type of content will be displayed. Then, it will search for a template to tell it how to do it. This is where WordPress template hierarchy comes into play. It essentially determines the order in which template files are chosen, going from very specific to increasingly general templates.
Say a visitor wants to visit http://avathemes.com/author/admin/. First, WordPress will look for the template named author-admin.php. If that particular template is missing, it will look for author.php. Going up still, it next looks for archive.php. Finally, if this template is also missing, WordPress will use index.php to load the page.