Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
One of the key components of any WordPress site is the index pages. Simply put, an index page shows a list of posts from the site depending on what you're asking for. The most well-known of the index pages is the blog page, which displays all the posts of the site in reverse chorological order. Other index pages include category index and archive indexes. The index pages in a WordPress theme or child theme live in a hierarchy and kick in at different times. Understanding which index page template handles and displays what content will help you customize the look and feel of your site and also avoid a lot of frustration when things don't display the way you intended.
If you look at this graphic of the WordPress template hierarchy, you can see how the different index pages kick in and when they kick in. What happens when you display any page in WordPress is the database and the browser cooperate to figure out what template to use. For example, if we're looking at a category page, this is what would happen. WordPress would say, "What kind of page is it?" It's an archive page and it's a category archive. "All right, do we have a file named category- and then the slug for this category, or the name for the category? If not, do we have a template called category- and then the number ID for that template? If not, do we have a file called category.php?" Chances are you do, and in that case we're going to use that category.php template.
The same goes for custom taxonomies, tags, author archives, date archives, and general archives. Looking at the 2012 folder, you'll see we have most of these different index page templates already in existence. We have the archive template; we have the author index template, the category index template. And if we scroll down, you'll see we have the regular index template. We also have a tag index template and even a search index template. All of these templates kick in when those specific types of content are asked for.
So let's take a look at how they work exactly. If you look at the index.php template, you see that off the very top, you have a commented-out section that explains what this template does, and it even has a link right to that graphic of the template hierarchy in WordPress. If you look at the body, you'll see exactly what happens when you use an index template. WordPress says, while you have posts, show each of the posts using the following template content. This means it's looking for a file called content- and then the name of the post format.
In older themes you won't see this get_template_part call; instead, you'll see the actual code to display all the contents for each post. But more recent themes use this function because it becomes easier to manage. So what happens here is, for each post we're actually calling this file called content and then we're displaying the content from that file. We need to look at content.php as well. You can see here, inside 2012, that we have a file called content.php and then we also have content files for each of the main post formats, aside, image, link, and so on.
We even have one for page. If we look at content, you'll see that all that happens here inside the loop is we display the content for each post. We get the post thumbnail, we get the title, we get the comments link, and we get either an excerpt or the contents, depending on what type of archive this is. So as you can see, just like with the page templates, the index page templates work in collaboration with the content templates to display all the content.
Understanding how the index pages work will help you come up with, and implement, new and exciting ways to display content. More importantly though, it'll help you troubleshoot potential problems when something doesn't display the way you intended, because now that you know how the template structure works, you can track down the correct template and make changes to that template when things aren't displaying correctly.
Get unlimited access to all courses for just $25/month.Become a member